Wednesday, June 24, 2009

Javascript Multiplication Error

When we try to multiply a decimal value with another decimal number or integer, javascript does not seem to handle this well.

for eg: 38.23 * 100 returns 3822.999999999995 instead of 3823.00

To solve this issue we need to use Math.round function

Solution:
Multiply the number by 1000, round it and then divide by 1000. This will give the expected result.

Math.round(1000 * 38.23 * 100) / 1000

Hope this solves the multiplication issue present in javascript and also your time.

No comments: