Bug in javascript with multiply -
this question has answer here:
- is floating point math broken? 20 answers
today have entered random float number , multipled hundred firstly using normal code , in console giving me wrong number, console returning me same.
the given float number is: 1050.6
therefore: 1050.6 * 100 should 105060, javascript returning me 105059.99999999999
anyone knows why?
javascript uses 64-bit floating point representation (double precision
). numbers represented in format whole number multiplied power of two.
this based on ieee 754 standard
rational numbers denominator isn't power of 2 can't represented. why floating point multiplication gives result.
source: https://en.wikipedia.org/wiki/ieee_floating_point
if want real value, there 2 methods can use
rounding math.round
math.round(1050.6 * 100)
or tofixed
(1050.6 * 1000).tofixed(0)
Comments
Post a Comment