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

Integers glitch after reaching a specific length #167

Closed
Jshauk opened this issue Jun 11, 2013 · 7 comments
Closed

Integers glitch after reaching a specific length #167

Jshauk opened this issue Jun 11, 2013 · 7 comments

Comments

@Jshauk
Copy link

Jshauk commented Jun 11, 2013

I tried typing a number in that is longer than 18 digits, it replaces all the numbers at the end with 0
I believe the problems lies within the highlighted code
code
Example:
error-example

@rxaviers
Copy link
Member

rxaviers commented Jun 3, 2014

Javascript Numbers can't hold integers bigger than Math.pow( 2, 53 ) (all numbers in JS are floats, more info). So, basically you can't get an integer bigger than 17 digits length with its full precision.

Globalize 1.x outputs the below, which I consider a regression compared to the previous output as shown in issue's description (I think the zero'ed result is better than the scientific notation one in this case, since user hasn't chosen so).

> Globalize.formatNumber(123456789012345)
'123,456,789,012,345'

> Globalize.formatNumber(123456789012345678901234567890)
'1.2345678901234568e+29'

Clearly, we'd need an additional big numbers library to handle that. Although, this should be optional.

@scottgonzalez, @jzaefferer, what do you think? Any ideas?

@rxaviers rxaviers added this to the 1.0.0 milestone Jun 3, 2014
@scottgonzalez
Copy link
Contributor

It's hard to say that this is a bug when JavaScript can't distinguish between these numbers.

123456789123456789123456 - 123456789123456789000000 === 0

Supporting a big number implementation is something we can consider, but I think we'd want to wait on that.

@jzaefferer
Copy link
Contributor

Can we throw an error instead of returning an incorrect value? input too long, must be smaller then Number.MAX or something like that?

@scottgonzalez
Copy link
Contributor

Number.MAX is massively larger than the point at which integers lose precision. I don't think it's possible to determine if the value that the user provided was accurately stored. For example, 9007199254740994 is greater than Math.pow( 2, 53 ), but is accurately stored. However, 9007199254740993 is smaller but isn't accurately stored.

@rxaviers rxaviers removed this from the 1.0.0 milestone Aug 27, 2014
@rxaviers
Copy link
Member

This seems a very hard problem. I'm closing as won't fix. But, I am open for ideas and code suggestions.

ashensis pushed a commit to ashensis/globalize that referenced this issue Mar 17, 2016
@SlexAxton
Copy link
Contributor

There is Number.MAX_SAFE_INTEGER now

@rxaviers
Copy link
Member

Thanks for the input

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

No branches or pull requests

5 participants