Problem using toFixed to round value

Asked

Viewed 369 times

1

I’m using the function toFixed() javascript to display values to two decimal places (toFixed(2) to display 2 digits after decimal point).

The problem occurs that I have two situations where the value has three digits after the decimal point, where it ends with the number 5 and the rounding behavior is different from both. One he applies the rule:

"If the digit before the decimal place you want to round is greater than or equal to 5, we should increase 1 in the decimal place chosen for rounding. If the number is less than 5, we just take out the decimals that don’t interest us, and the number doesn’t change."

Only the other doesn’t apply.

Example

console.log('Resultado: ' + 6.825.toFixed(2))
console.log('Resultado: ' + 4.925.toFixed(2))

Both should have the same behavior, someone has been through this problem?

Observing: I’ve tried using the function toPrecision() but didn’t solve for me.

1 answer

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.