Javascript: why does this produce and ugly string??? I would like currency

只谈情不闲聊 提交于 2019-12-04 05:58:49

.toFixed converts the object from a Number to a String.

Leave the full values in place and only convert using .toFixed at the very end

$(".total").text(total.toFixed(2));

Alternatively, convert the string back to a number.

total = total + + tmp;

Just FYI, there is an excellent mathematical aggregation plugin for jQuery: jQuery Calculation

Using that plugin may also indirectly solve your issue.

It's usage would reduce your script to:

$('.total').text($('.amount').sum());

You are converting the parseFloat into a string, then adding it to total. Only add .toFixed(2) to the final line, once things have been added.

var total = 0;
$(".amount").each(function() {
    var value = $(this).val();
    value = (value.length < 1) ? 0 : value;
    var tmp = parseFloat(value);
    total += tmp;
});
$(".total").text(total).toFixed(2);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!