开发者

javascript float arithmetic

well, I've a javascript code which uses two arrays:

  1. items [id,name, price]

    prices are float from mysql db.

  2. representation of an order in html form which i iterate us开发者_开发技巧ing jquery

now I compute the total price

I run over the second table and retrieves proper price from the first.

but I get the answer+0.000000001.

which i use tofixed to cut.

but still I'm puzzled if this is a known behavior.


yes. it's known behavior, and the good example

0.1 + 0.2 = 0.30000000000000004


Could you update your DB query to multiply the price by 100 and so return it in (whatever your smallest unit of current is... pence, cents, etc)? That way you're dealing with integers and can just format your output when you want to display it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜