cowboy / jquery-bbq

jQuery BBQ: Back Button & Query Library
http://benalman.com/projects/jquery-bbq-plugin/
GNU General Public License v2.0
1.18k stars 207 forks source link

deParam() mangles large numbers #28

Open sebkun opened 13 years ago

sebkun commented 13 years ago

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 commented 13 years ago

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 commented 13 years ago

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 commented 12 years ago

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