Javascript 为什么我们需要使用基数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6611824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:28:21  来源:igfitidea点击:

Why do we need to use radix?

javascript

提问by John Cooper

What does radix actually means? Why do we need it?

基数实际上是什么意思?为什么我们需要它?

parseInt(10, radixValue); 

回答by Justin Niessner

You might not always want to parse the integer into a base 10 number, so supplying the radix allows you to specify other number systems.

您可能并不总是希望将整数解析为基数为 10 的数字,因此提供基数允许您指定其他数字系统。

The radix is the number of values for a single digit. Hexidecimal would be 16. Octal would be 8, Binary would be 2, and so on...

基数是单个数字的值数。十六进制为 16。八进制为 8,二进制为 2,依此类推...

In the parseInt()function, there are several things you can do to hint at the radix without supplying it. These can also work against you if the user is entering a string that matches one of the rules but doesn't expressly mean to. For example:

在该parseInt()函数中,您可以执行多种操作来提示基数而不提供基数。如果用户输入的字符串与其中一个规则匹配但没有明确的意思,这些也可能对您不利。例如:

// Numbers with a leading 0 used a radix of 8 (octal) before ECMAScript 5.
// These days, browsers will treat '0101' as decimal.
var result = parseInt('0101');

// Numbers that start with 0x use a radix of 16 (hexidecimal)
var result = parseInt('0x0101');

// Numbers starting with anything else assumes a radix of 10
var result = parseInt('101');

// Or you can specify the radix, in this case 2 (binary)
var result = parseInt('0101', 2);

回答by Michael Berkowski

Because if you have a string number like 0700and want the output to be integer 700you need to inform parseInt()that it is a decimal number rather than octal.

因为如果你有一个类似的字符串数字0700并且希望输出是整数,700你需要通知parseInt()它是一个十进制数而不是八进制数。

console.log(parseInt("0700"));
// 448

// I really wanted decimal (base 10)
console.log(parseInt("0700", 10));
// 700

// What is this? Binary, Decimal, Octal?
console.log(parseInt("0110"));
// 72

// as binary
console.log(parseInt("0110", 2));
// 6

NoteI only answered half your question. See others for good definitions of what a radix actually is.

注意我只回答了你的问题的一半。有关基数实际上是什么的良好定义,请参阅其他人。

回答by George Cummins

Radix is the base of a system of numeration. There are an infinite number of numeric systems but the ones with which most people are familiar are base 10 (decimal) and base 2 (binary).

基数是计数系统的基础。数字系统有无数种,但大多数人熟悉的是以 10 为底(十进制)和以 2 为底(二进制)的系统。

Numeric values can be interpreted differently in different bases. For example, the number 10 in binary can be represented as 2 in decimal.

数值在不同的基础上可以有不同的解释。例如,二进制中的数字 10 可以表示为十进制中的 2。

In the case of parseInt(), the radix allows you to specify the base to be used. By default, a radix of 10 is used.

在 的情况下parseInt(),基数允许您指定要使用的基数。默认情况下,使用基数 10。

However, the radix should always be specified, even when using base 10. Consider the case of

但是,即使使用基数 10,也应始终指定基数。请考虑以下情况

parseInt("010")// Returns 8

parseInt("010")// 返回 8

At first glance, you may expect the statement to return 10. Explicit use of the radix will help to avoid confusion:

乍一看,您可能期望该语句返回 10。显式使用基数将有助于避免混淆:

parseInt("010", 10)// Returns: 10

parseInt("010", 10)// 返回:10

回答by Mathias Schwarz

The radix is the base number of the number system: http://en.wikipedia.org/wiki/Radix

基数是数字系统的基数:http: //en.wikipedia.org/wiki/Radix

Normally, you only need to specify the radix if you want it to be different from 10. More specifically (from http://www.w3schools.com/jsref/jsref_parseInt.asp) :

通常,如果您希望它与 10 不同,则只需要指定基数。更具体地说(来自http://www.w3schools.com/jsref/jsref_parseInt.asp):

If the radix parameter is omitted, JavaScript assumes the following:

If the string begins with "0x", the radix is 16 (hexadecimal) If the string begins with "0", the radix is 8 (octal). This feature is deprecated If the string begins with any other value, the radix is 10 (decimal)

如果 radix 参数被省略,JavaScript 假定如下:

如果字符串以“0x”开头,则基数为 16(十六进制) 如果字符串以“0”开头,则基数为 8(八进制)。此功能已弃用 如果字符串以任何其他值开头,则基数为 10(十进制)

回答by nmoliveira

Just adding some additional information which clearly answers the question:

只需添加一些附加信息即可清楚地回答问题:

If radix is undefined or 0 (or absent), JavaScript assumes the following:

如果 radix 未定义或为 0(或不存在),则 JavaScript 假定如下:

  • [...]
  • If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.
  • [...]
  • [...]
  • 如果输入字符串以“0”开头,则基数为八(八进制)或 10(十进制)。究竟选择哪个基数取决于实现。 ECMAScript 5 指定使用 10(十进制),但并非所有浏览器都支持它。由于这个原因,在使用 parseInt 时总是指定一个基数。
  • [...]

Source: MDN parseInt()

来源:MDN parseInt()

回答by Konstantin Smolyanin

It's just my opinion but idea that "we need to use radix" quickly becomes outdated. The problem really was actual some time ago because people outside IT world usually don't use number notations other than decimal and quite often provide decimal numbers zero-padded like "010". But since ECMAScript 6 octal numbers in JS are prefixed by "0o" and not just "0" as it was in ECMAScript 5 and 3. So if you don't target IE family (that is not rare situation now) you can skip radix safely.

这只是我的意见,但“我们需要使用基数”的想法很快就过时了。前一段时间确实存在这个问题,因为 IT 世界以外的人通常不使用十进制以外的数字符号,并且经常提供十进制数字填充零,如“010”。但是由于 JS 中的 ECMAScript 6 八进制数以“0o”为前缀,而不仅仅是 ECMAScript 5 和 3 中的“0”。因此,如果您不针对 IE 系列(现在这种情况并不罕见),您可以跳过基数安全。

回答by Jeremy Seekamp

The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number.

If the radix parameter is omitted, JavaScript assumes the following:

If the string begins with "0x", the radix is 16 (hexadecimal)

If the string begins with "0", the radix is 8 (octal). This feature is deprecated

If the string begins with any other value, the radix is 10 (decimal)

radix 参数用于指定使用哪种数字系统,例如基数为 16(十六进制)表示字符串中的数字应从十六进制数解析为十进制数。

如果 radix 参数被省略,JavaScript 假定如下:

如果字符串以“0x”开头,则基数为16(十六进制)

如果字符串以“0”开头,则基数为 8(八进制)。此功能已弃用

如果字符串以任何其他值开头,则基数为 10(十进制)

Source W3Schools

来源W3Schools