Problem using toFixed to round value

1

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

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

"If the number prior to the decimal point you want to round is greater than or equal to 5, you must increase by 1 in the decimal place chosen for rounding. If the number is less than 5, decimal places that do not interest us, and the number does not change. "

Only the other does not apply.

Example

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

Both of them should have the same behavior, has anyone ever had this problem?

Note: I've tried using the toPrecision () function but it did not work for me.

    
asked by anonymous 23.08.2018 / 14:50

1 answer

0

By doing other searches, I was able to solve the problem using Math.round ()

For the rounding problem due to imprecise floating-point arithmetic, it was necessary to use the function as follows

Math.round (# 100) / 100

console.log(Math.round(4.925 * 100) / 100)
console.log(Math.round(6.825 * 100) / 100)

Source: link

    
23.08.2018 / 15:48