How to multiply in Javascript? problems with decimals

gustavomanolo picture gustavomanolo · Jan 23, 2013 · Viewed 59.4k times · Source

i've the following code in Javascript:

var m1 = 2232.00;
var percent = (10/100);
var total = percent*m1;
alert(total);

The problem is that the variable "total" gives me "223.20000000000002" and it should be "223.2", what should i do to get the correct value?

Answer

Malik Khalil picture Malik Khalil · Jul 29, 2015

.toFixed() is best solution.It will keep only two digits after dot.

Exp 1:

var value = 3.666;
value.toFixed(2); //Output : 3.67

Exp 2:

var value = 3.0000;
value.toFixed(2); //Output : 3.00