0

I am using this code to get float with only two number after the . :

var number = 1.9090909090909092;
var newNumber = (number).toFixed(2);

And in newNumber i get : 1.91 instead of 1.90. Any idea why it happen?

YosiFZ
  • 7,792
  • 21
  • 114
  • 221

2 Answers2

3

if you want to format a float to any number of digits use this function:

function truncate(num, pos) {

    return Math.floor(num*Math.pow(10, pos))/Math.pow(10, pos);

}

where num is the float and pos is the number of digits you want after the decimal.

cpolito
  • 76
  • 3
-1

He approximate The number

Example

25.65 = 25.7

5.699 = 5.70