使用JavaScript从字符串中删除逗号 [英] Remove commas from the string using JavaScript
本文介绍了使用JavaScript从字符串中删除逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从字符串中删除逗号并使用JavaScript计算这些数量。
I want to remove commas from the string and calculate those amount using JavaScript.
例如,我有两个值:
- 100,000.00
- 500,000.00
现在我想从这些字符串中删除逗号,并希望获得这些字符串的总数。
Now I want to remove commas from those string and want the total of those amount.
推荐答案
要删除逗号,你将会需要使用 替换
在字符串上。要转换为浮点数以便进行数学运算,您需要 parseFloat
:
To remove the commas, you'll need to use replace
on the string. To convert to a float so you can do the maths, you'll need parseFloat
:
var total = parseFloat('100,000.00'.replace(/,/g, '')) +
parseFloat('500,000.00'.replace(/,/g, ''));
这篇关于使用JavaScript从字符串中删除逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文