views:

67

answers:

3

How to parse 20 digit number using java script, jquery?.

A: 

normally you use Number() or parseInt("", [radix]) to parse a string into a real number.

I am guessing you are asking about what happens when the string you parse is above the int - threshold. in this case it greatly depends on what you are trying to accomplish.

There are some libraries that allow working with big numbers such as http://www.leemon.com/crypto/BigInt.js (did not test it, but it looks ok). Also try searching for BigInt javascript or BigMath.

In short: working with VERY large number or exact decimals is a challenge in every programming language and often requires very specific mathematical libraries (which are less convenient and often a lot slower then when you work in "normal" (int/long) areas) - which obviously is not an issue when you REALLY want those big numbers.

Niko
Javcascript doesn't have an "int" threshold because it doesn't have integers - all numbers are IEEE floating point.
Pointy
A: 

A 20-digit number is generally too big for a Javascript native numeric type, so you'll need to find a "big number" package to use. Here's one that I've seen mentioned on SO and that looks interesting: http://silentmatt.com/biginteger/

That one is just integers. If you need decimal fractions too, you'll have to find something else.

If you just want to check that a 20-digit string looks like a number, and you don't need to know the value, you can do that with a regular expression.

Pointy
A: 

You can't have a 20 digit number in javascript- it'll get floated off on you.

You can keep 20 digits (or 200 or 2000) intact as a string, or as an array of digits, but to do any math on it you need a big Integer object or library.

kennebec