Duke Ngboki Duke Ngboki - 6 months ago 25
HTML Question

How do you sum two values containing the dollar sign "$"? I have been trying to figure out the methods and properties, still no clue

//for example
allInputs[22].value //equals $45 and
allInputs[4].value // equals $70

allInputs[22].value + allInputs[4].value = "$45$70"

It equals "$45$70". But I only want to sum up the value. how do I sum up both values to get the final value ignoring the dollar sign?

Answer Source

You can use

"$" + (parseInt(allInputs[22].value.substring(1)) + parseInt(allInputs[22].value.substring(1)))

The substring method, will get rid of the $ sign, and parseInt will convert it to a number. You need that, because if you do not use that it will concatenate the values as strings. Note that i put another set of brackets to sum the numbers. That is because, when the interpreter sees the "$" it thinks it should concatenate strings. But we want to sum the numbers and then concatenate the sum with the "$" string.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download