How to parse 20 digit number using JavaScript and jQuery

后端 未结 3 1969
Happy的楠姐
Happy的楠姐 2020-12-03 23:41

How do I parse a 20-digit number using JavaScript and jQuery?

相关标签:
3条回答
  • 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 Stack Overflow 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.

    0 讨论(0)
  • 2020-12-04 00:22

    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 https://silentmatt.com/biginteger/ - see answer - (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 than when you work in "normal" (int/long) areas) - which obviously is not an issue when you REALLY want those big numbers.

    0 讨论(0)
  • 2020-12-04 00:38

    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.

    0 讨论(0)
提交回复
热议问题