Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

formatNumber produces NaN for numbers near 999999999999999934463 #12707

Closed
@danprince

Description

@danprince

At the moment, the formatNumber method does some hand built rounding for decimal numbers, in order to not lose precision.

It uses the e (scientific) notation (e.g. 1000e2) to shift the number and remove unnecessary precision, before rounding it and then doing the inverse 100000e-2 to return the number to it's original state.

If the number is already in scientific notation then it is diverted away beforehand. However, if the number is not in scientific notation, but scaling it up with 'e' + fractionSize will take it above the browser's limit (999999999999999934463) then the string ends up with two es.

For example, take 444444444400000000000 and lets say we want to round it to 2 points of precision.

+(444444444400000000000 + 'e' + 2)
// becomes scientific notation
4.444444444e+22

// then we round it
Math.round(4.444444444e+22)
4.444444444e+22

// then we scale it back
4.444444444e+22 + 'e' + -2
"4.444444444e+22e-2"

// this version has two exponents and can't be coerced with +
+("4.444444444e+22e-2")
NaN

I think this issue may have already been opened as #8674, but the conditions for reproducing are a lot less clear in that issue.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions