2

I find a biggest problem with toFixed(2) i.e. if i write 5.555 then it will display 5.55 and if I write 5.565 then it will display 5.57. What should I do?

This is what i am doing. Declaring one array and toFixed all values of first array and put in second array.

var arr1 = [25.205,25.215,25.225,25.235,25.245,25.255,25.265,25.275,25.285,25.295]

var arr2 = []

for(i=0;i<10;i++){ arr2[i]= +arr1[i].toFixed(2) }

Results:

arr1 = [25.205, 25.215, 25.225, 25.235, 25.245, 25.255, 25.265, 25.275, 25.285, 25.295]

arr2 = [25.2, 25.21, 25.23, 25.23, 25.25, 25.25, 25.27, 25.27, 25.29, 25.3]

should i have to use the Math.floor() method of the Math object for this.

Math.floor(number*100)/100
Kaushal Khamar
  • 2,097
  • 1
  • 17
  • 23

2 Answers2

1

This is due to the binary representations of the fractions being slightly more or less than the numbers entered. See the accepted answer HERE for an alternative method of rounding to 2 significant figures.

Community
  • 1
  • 1
sideroxylon
  • 4,338
  • 1
  • 22
  • 40
0

I have create a function which done all for me..

function toFixed(number, precision) {
    var multiplier = Math.pow(10, precision + 1),
        wholeNumber = Math.floor(number * multiplier);
    return Math.round(wholeNumber / 10) * 10 / multiplier;
}

//Call this function to retrive exect value
toFixed((+adjustmentval), 2);
Kaushal Khamar
  • 2,097
  • 1
  • 17
  • 23