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

deParam() mangles large numbers #28

Open
sebkun opened this issue Feb 2, 2011 · 3 comments
Open

deParam() mangles large numbers #28

sebkun opened this issue Feb 2, 2011 · 3 comments

Comments

@sebkun
Copy link

sebkun commented Feb 2, 2011

Part of the deParam() code tries to parse numeric values into Javascript numbers. However, since javascript stores all numbers as doubles, any number larger than around 2^53 will suffer a loss of precision when converted.

E.g.:
var longNum = "123456789012346578901234567980";
var urlStr = "http://www.example.com/?key=" + longNum;
var url = $.url.parse(urlStr);
alert(url.params['key']); // 1.2345678901234658e+29

In my code I worked around this by changing:
val = val && !isNaN(val) ? +val // number
to
val = val && !isNaN(val) && val < 0x20000000000000 ? +val // number

@cowboy
Copy link
Owner

cowboy commented Feb 3, 2011

If you don't want $.deparam to attempt to coerce strings into numbers, you can simply not specify the coerce argument. Have you tried that?

@sebkun
Copy link
Author

sebkun commented Feb 4, 2011

Good point, although then it doesn't convert other values and in my use case the loss of precision for large numbers (random 64-bit IDs) was undesirable.

@willemstuursma
Copy link

Javascript does not support numbers > 2^53, you should keep them as strings in that case.

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

3 participants