admin管理员组文章数量:1026645
I have created function after button click:
var cena_poczatkowa = parseFloat($("#cena_aktualna").text());
var cena_dodana = cena_poczatkowa + 1.01;
$("span#cena_aktualna").text(cena_dodana);
And span in html:
<span id="cena_aktualna">0.00</span>
Everything working fine, after every click number is changed in span: 1.01, 1.02. But after thrid click I see 3.0300000000000002. After fourth click I see again properly 4.04. Why after third click I see this strange number?
Here is my working script so you can see this bug: /
I have created function after button click:
var cena_poczatkowa = parseFloat($("#cena_aktualna").text());
var cena_dodana = cena_poczatkowa + 1.01;
$("span#cena_aktualna").text(cena_dodana);
And span in html:
<span id="cena_aktualna">0.00</span>
Everything working fine, after every click number is changed in span: 1.01, 1.02. But after thrid click I see 3.0300000000000002. After fourth click I see again properly 4.04. Why after third click I see this strange number?
Here is my working script so you can see this bug: http://jsfiddle/H3pfH/
Share Improve this question asked Aug 23, 2011 at 12:52 LucasLucas 3,0048 gold badges28 silver badges32 bronze badges 1- 1 Possible duplicate (stackoverflow./questions/1458633/…). – maerics Commented Aug 23, 2011 at 12:55
1 Answer
Reset to default 5Since floating point math is inherently imprecise, try using toFixed() to round it to a suitable number of digits:
var cena_dodana = (cena_poczatkowa + 1.01).toFixed(4);
http://jsfiddle/H3pfH/1/
I have created function after button click:
var cena_poczatkowa = parseFloat($("#cena_aktualna").text());
var cena_dodana = cena_poczatkowa + 1.01;
$("span#cena_aktualna").text(cena_dodana);
And span in html:
<span id="cena_aktualna">0.00</span>
Everything working fine, after every click number is changed in span: 1.01, 1.02. But after thrid click I see 3.0300000000000002. After fourth click I see again properly 4.04. Why after third click I see this strange number?
Here is my working script so you can see this bug: /
I have created function after button click:
var cena_poczatkowa = parseFloat($("#cena_aktualna").text());
var cena_dodana = cena_poczatkowa + 1.01;
$("span#cena_aktualna").text(cena_dodana);
And span in html:
<span id="cena_aktualna">0.00</span>
Everything working fine, after every click number is changed in span: 1.01, 1.02. But after thrid click I see 3.0300000000000002. After fourth click I see again properly 4.04. Why after third click I see this strange number?
Here is my working script so you can see this bug: http://jsfiddle/H3pfH/
Share Improve this question asked Aug 23, 2011 at 12:52 LucasLucas 3,0048 gold badges28 silver badges32 bronze badges 1- 1 Possible duplicate (stackoverflow./questions/1458633/…). – maerics Commented Aug 23, 2011 at 12:55
1 Answer
Reset to default 5Since floating point math is inherently imprecise, try using toFixed() to round it to a suitable number of digits:
var cena_dodana = (cena_poczatkowa + 1.01).toFixed(4);
http://jsfiddle/H3pfH/1/
本文标签: jQueryjavascriptHow add decimal numbersStack Overflow
版权声明:本文标题:jQuery, javascript - How add decimal numbers? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1744282664a2089555.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论