Why toFixed function in nodejs result difers between slightly different value of floating point?

82 Views Asked by At

I having a problem to rounded a decimal into two number behind comma using to fixed

i tried this.

console.log(24.675.toFixed(2))
console.log(31.675.toFixed(2))

It return (this is correct)

24.68
31.68

I tried to used

console.log(24.675.toFixed(2))
console.log(31.674.toFixed(2))

it return (still correct):

24.68
32.67

But when i tried

console.log(24.675.toFixed(2))
console.log(32.675.toFixed(2)) <- its 32 before comma instead 31 (first case)

It return (false):

24.68
32.67

Why is that ? i thought 32.675 will return 32.68, but instead it return 32.67

How do i make a correct rounded floating point like this ?

0

There are 0 best solutions below