javascript解析浮动错误 [英] javascript parse float error
本文介绍了javascript解析浮动错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取表格的行数:
I am trying to get sum of rows of my table:
td1 val = $ 5,000.00; td2 val = $ 3000.00;
td1 val = $5,000.00; td2 val = $3000.00;
我使用以下代码:
var totalnum = 0;
$('.num').each(function(){
totalnum+= parseFloat($(this).html());
});
$('.total_num').html(totalnum);
如果我从号码中删除货币格式,此代码非常有用,否则它会给出 NaN
结果即使我使用 parseFloat
。
This code works perfect if I remove money formatting from the number, otherwise it gives NaN
as a result even if I am using parseFloat
.
我是什么缺少?
推荐答案
尝试:
var totalnum = 0;
$('.num').each(function(){
totalnum+= parseFloat($(this).html().substring(1).replace(',',''));
});
$('.total_num').html('$' + totalnum);
在执行parseFloat之前,这将从开头和所有逗号中删除$(或任何货币符号)然后把它换回总数。
This will remove the $ (or whatever currency symbol) from the beginning and all commas before doing the parseFloat and put it back for the total.
或者你可以使用 jQuery FormatCurrency 插件并执行此操作:
Alternatively you could use the jQuery FormatCurrency plugin and do this:
totalnum+= $(this).asNumber();
这篇关于javascript解析浮动错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文