我有代码,我在 jquery 中使用它
var string = "123,123";
$.trim(",");
让我在源代码中展示它:
function calculate_total_money() {
sum_money = 0;
$('table#table_product tr.data td#total_money').each(function() {
var _total_money = $(this).html();
if (_total_money != '') {
var total_money = _total_money;
sum_money += total_money;
}
});
$('table#table_product td#sum_total_money').html(formatNumber2(sum_money));}
这个函数返回总金额,但是我在“23,123”这样的表中得到_total_money的值,但我想将其转换为Int 23123。我不知道该怎么做。
我想删除字符串中的逗号,但它不起作用。删除逗号后,字符串必须转换为整数。我尝试了很多次但没有任何反应。我哪里做错了?
最佳答案
如果字符串中有单个 ,
,请使用 replace
:
string = parseInt(string.replace(',', ''), 10);
注意:replace
只会替换第一次出现的,
。
如果字符串中有多个,
,请在replace
中使用regex
:
string = parseInt(string.replace(/,/g, ''), 10);
parseInt
将字符串
转换为整数
。
关于jquery - jquery中将字符串转换为int并删除字符串中的逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30796651/