-1

I am aware this question has been asked before and i have read up on the subject, but still the solution isn't clear to me. And my attempts fails.

<div class="subtotal" data-content="€">30.50</div>

but with the following jquery js it reads 30.509

test = parseFloat($('.subtotal').text());//reads 30.509

i tried

test = parseFloat(Number($('.subtotal').text()).toFixed(2));//gives NaN

How do i make sure javascript reads the subtotal as 30.50 and not 30.509?

Regards,

4

2 回答 2

1

它不会读取不同的值。不同之处在于浮点数在内存中存储方式的精度。当您存储一个号码时,您无需担心这一点。您将永远无法完全按照您的意愿存储它。

显示数字时,您必须将其修剪为两位小数,这样在读取时应正确显示。您的问题只是关于数字的显示方式。

于 2015-05-18T11:12:28.687 回答
0

我看到了冗余,为什么 parseFloat 一个已经是 a 的值Number

只是parseFloat文本,应该完成工作。

var str_subtotal = "12.348";
var num_subtotal = parseFloat(str_subtotal); // 12.348 (as number)
console.log(num_subtotal.toFixed(2));

请注意 toFixed不会对数字进行四舍五入,Math.round而是使用

于 2015-05-18T11:15:16.650 回答