This is because numbers are represented internally as binary numbers with limited precision.
See also Is floating point math broken?
Is floating point math broken?
0.1 + 0.2 = 0.3 β false
0.1 + 0.2 β 0.30000000000000004
Any ideas why this is happening?
What answer did you get:
Binary floating point math like this. In most programming languages, it is based on the IEEE 754 standard . JavaScript uses a 64-bit floating-point representation, which is the same as Java double . The essence of the problem is that the numbers are presented in this format as an integer number twice; rational numbers (such as 0.1 , which is 1/10 ), whose denominator is not a power of two, cannot be accurately represented.
To get the correct result in your case, you need to round after all arithmetic:
var num = 0.056789, roundingPrecision = 4, roundedNum = _.round(num * 100, roundingPrecision), percent = roundedNum + '%'; console.log(percent);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"></script>
source share