简体   繁体   English

如何使用JavaScript和jQuery解析20位数字

[英]How to parse 20 digit number using JavaScript and jQuery

如何使用JavaScript和jQuery解析20位数字?

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. 对于JavaScript本机数字类型,通常20位数字太大,因此您需要找到一个“大数字”包才能使用。 Here's one that I've seen mentioned on Stack Overflow and that looks interesting: http://silentmatt.com/biginteger/ 这是我在堆栈溢出中提到的内容,看起来很有趣: 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. 如果只想检查一个20位数字的字符串看起来像一个数字,而又不需要知道该值,则可以使用正则表达式来实现。

Normally you use Number() or parseInt("", [radix]) to parse a string into a real number. 通常,您使用Number()或parseInt(“”,[radix])将字符串解析为实数。

I am guessing you are asking about what happens when the string you parse is above the int - threshold. 我猜您正在询问解析的字符串超过int-阈值时会发生什么。 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). 有一些库可以处理较大的数字,例如https://silentmatt.com/biginteger/-请参阅答案-(没有测试,但看起来还可以)。 Also try searching for BigInt JavaScript or BigMath. 另外,请尝试搜索BigInt JavaScript或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. 简而言之:在每种编程语言中,使用非常大的数字或精确的小数都是一个挑战,并且经常需要非常特定的数学库(与在“常规”(整数/整数)区域中工作相比,它不那么方便,而且通常要慢得多) -当您真正想要这么大的数字时,这显然不是问题。

You can't have a 20-digit number in JavaScript - it'll get floated off on you. 您不能在JavaScript中使用20位数字 -它会浮在您身上。

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. 您可以将20个数字(或200或2000)保留为一个字符串或一个数字数组,但要对其进行任何数学运算,您需要一个大的整数对象或库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM