Currency Math in JavaScript

后端 未结 3 721
广开言路
广开言路 2020-12-16 14:30

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

3条回答
  •  被撕碎了的回忆
    2020-12-16 14:48

    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.

提交回复
热议问题