Bug in javascript with multiply -


this question has answer here:

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

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -