Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

result of BigDecimal toFixed should be different from Number toFixed #72

Open
yukinotech opened this issue Dec 20, 2021 · 1 comment
Open

Comments

@yukinotech
Copy link

In node or chrome(v8) , when the result of toFixed is rounded, the result is not accurate . because number is float

console.log((1.05).toFixed(1)) // 1.1
console.log((1.15).toFixed(1)) // 1.1
console.log((1.25).toFixed(1)) // 1.3
console.log((1.35).toFixed(1)) // 1.4
console.log((1.45).toFixed(1)) // 1.4
console.log((1.55).toFixed(1)) // 1.4
console.log((1.65).toFixed(1)) // 1.6
console.log((1.75).toFixed(1)) // 1.8
console.log((1.85).toFixed(1)) // 1.9
console.log((1.95).toFixed(1)) // 1.9

however , in BigDecimal ,we can claculate an exact value by roundMode

// default use `half up` roundMode
console.log((1.05m).toFixed(1)) // 1.1
console.log((1.15m).toFixed(1)) // 1.2
console.log((1.25m).toFixed(1)) // 1.3
console.log((1.35m).toFixed(1)) // 1.4
console.log((1.45m).toFixed(1)) // 1.5
console.log((1.55m).toFixed(1)) // 1.6
console.log((1.65m).toFixed(1)) // 1.7
console.log((1.75m).toFixed(1)) // 1.8
console.log((1.85m).toFixed(1)) // 1.9
console.log((1.95m).toFixed(1)) // 2.0

The same problem appeared in BigDecimal.prototype.toExponential

@Yaffle
Copy link

Yaffle commented Dec 20, 2021

@yukinotech
1.05 in the source code is converted to 1.0500000000000000444089209850062616169452667236328125 floating point value, then Number#toFixed is applied;
Both algorithms are defined to work with exact mathematical value.

Well, there is a problem with Intl.NumberFormat, where they want to look at 1.0500000000000000444089209850062616169452667236328125 as the 1.05 and so do the "toRawFixed" differently, and the spec is still not defined well (see tc39/ecma402#128)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants