Can someone please help me out with a JavaScript/jQuery solution for this arithmetic problem:
I need to subtract one number from the other.
The problem is that the numbers have a dollar sign (because its money), therefore jQuery is treating them as strings instead of numbers.
I have created two variables - toalAssets
and totalLiabilites
. I would like to subtract the latter from the former and place the result into another variable called netWorth
.
Perhaps i need to use parseFloat()
?
But I'm not sure how - This is all a little over my head!
Can someone please help me out with a JavaScript/jQuery solution for this arithmetic problem:
I need to subtract one number from the other.
The problem is that the numbers have a dollar sign (because its money), therefore jQuery is treating them as strings instead of numbers.
I have created two variables - toalAssets
and totalLiabilites
. I would like to subtract the latter from the former and place the result into another variable called netWorth
.
Perhaps i need to use parseFloat()
?
But I'm not sure how - This is all a little over my head!
Share Improve this question edited Apr 1, 2010 at 9:51 Daniel Vassallo 344k72 gold badges512 silver badges446 bronze badges asked Mar 31, 2010 at 20:44 swisstonyswisstony 1,7153 gold badges18 silver badges28 bronze badges 2- @Mark: he's only asked 5 questions and has 36 rep, so is obviously new to the site. – DisgruntledGoat Commented Mar 31, 2010 at 20:47
- 3 @Jared: You can go back to your old questions (click your username at the top) and accept answers on them if appropriate. More people will be likely to answer future questions. – DisgruntledGoat Commented Mar 31, 2010 at 20:48
3 Answers
Reset to default 32var totalLiabilites = '$52.34';
var toalAssets = '$85.12';
var pattern = /[^0-9.-]+/g;
var result = parseFloat(toalAssets.replace(pattern, '')) -
parseFloat(totalLiabilites.replace(pattern, ''));
// result: 32.78
Note: In JavaScript it is recommended1 to handle money as an integer representing the number of cents (8512 instead of 85.12). This is to avoid problems with floating-point arithmetic. 0.1 + 0.2 == 0.3
returns false in JavaScript, but fortunately integer arithmetic in floating point is exact, so decimal representation errors can be avoided by scaling.
You may want to check the following post for further reading on this topic: Is JavaScript’s math broken?
You can always apply the currency-sign formatting when the values are rendered to the browser.
1Douglas Crockford: JavaScript: The Good Parts: Appendix A - Awful Parts (page 105).
parseFloat()
won't work because your string begins with a non-number, the dollar sign.
You can simply do a replace to remove the dollar sign, along with a parseFloat to get the value:
totalAssets = parseFloat(totalAssets.replace('$', ''));
totalLiabilities = parseFloat(totalLiabilities.replace('$', ''));
var difference = '$' + (totalAssets - totalLiabilities);
This code replaces your original strings with floats. You could load them into new variables as well. Likewise, difference does not have to have the '$' prepended.
var a = "$20";
var b = "$34";
var c = parseFloat(a.replace(/[^0-9\.]+/g, "")) - parseFloat(b.replace(/[^0-9\.]+/g, ""));
alert(c);