Is there a definitive solution to javascript floating-point errors?

后端 未结 3 517
無奈伤痛
無奈伤痛 2020-12-19 14:09

I write line of business applications. I\'d like to build a front-end end using Javascript and am trying to figure out how to deal with, for a business user, are floating p

3条回答
  •  时光取名叫无心
    2020-12-19 14:29

    According to Douglas Crockford, the only way around this problem is scale your values to integer. Make sure it really is an integer by using Math.round on the scaled value. (DC does not talk about the rounding part, but I discovered it was necessary. e.g. Math.round(1.1 *100)) Do calculation(s). When you are done with the math scale back to original precision. See JavaScript: The Good Parts "Floating Point" section.

提交回复
热议问题