This is a famous problem in computer world. It is originated from the IEEE 754 spec and binary system. Let’s see the problem at JavaScript:
var a = 0.2 + 0.4;
console.log(a);
In most browser, we may have the value 0.600…0001. It is very anonying when we want to show a value with decimal point. There are a lot of methods to deal with it. We focus on round
and parseFloat
:
-
Math.round(a * 10) / 10
-
parseFloat(a.toFixed(1))
I believe that you will say method 1 has high performance. But how faster it has?
Please see the result here: http://jsperf.com/round-vs-tofix-parsefloat