-1

我不明白这里发生了什么:

var x = 14;
alert(x.toString(36)); //alerts 'e'

alert(x.toString(16)); //also alerts 'e'

x = 20;
alert(x.toString(16)); //alerts '14'
alert(x.toString(36)); //alerts 'k'

我认为第一个参数决定了要转换的数字的数值系统,但我不确定。任何人都可以详细解释到底发生了什么?

4

3 回答 3

3

您正在通过传递 number 参数来更改基数。第二个例子最容易解释。. . base16 中的 14 是“e”。base16 的“数字”是 0、1、2、3、4、5、6、7、8、9、a (10)、b (11)、c (12)、d (13)、e (14) , 和 f (15)。

对于任何 15 或以上的数字系统,实际上,14 将 = 'e'。

在 20 的情况下,base16 给你 14,因为你有 116,再加上 4 个 (16 + 4 = 20)。

对于水平集,十进制(base10 ......我们最习惯的系统),14 在技术上是 1 十加 4。

如果您不熟悉不同的编号系统,可能需要一点时间来适应。:)

于 2013-03-01T16:12:08.183 回答
2

The parameter is the radix.

number.toString( [radix] )

See https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Number/toString

The Number object overrides the toString method of the Object object; it does not inherit Object.toString. For Number objects, the toString method returns a string representation of the object in the specified radix.

The toString method parses its first argument, and attempts to return a string representation in the specified radix (base). For radixes above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), a through f are used.

If toString is given a radix not between 2 and 36, an exception is thrown.

If the radix is not specified, the preferred radix is assumed to be 10.

于 2013-03-01T16:07:19.900 回答
0

可选参数是radix

于 2013-03-01T16:11:02.977 回答