JavaScript multiplying by 100 giving weird result

user3213420 picture user3213420 · Jan 31, 2014 · Viewed 11.7k times · Source

I have:

var a =  0.0532;
var b =  a * 100;

b should be returning 5.32 but instead it's returning 5.319999999999999. How do I fix this?

JSFiddle here: http://jsfiddle.net/9f2K8/

Answer

yashhy picture yashhy · Jan 31, 2014

you should use .toFixed()

FIDDLE

var a =  0.0532;
var b =  a * 100;
b.toFixed(2);     //specify number of decimals to be displayed