Javascript - 将数字转换为将数字转换为某个不同数字的数字。不明白为什么会这样? [英] Javascript - Leading zero to a number converting the number to some different number. not getting why this happening?
问题描述
前导零到某个数字将数字转换为某种未知数字格式。
例如:
A leading zero to some number converting the number to some unknown number format. for example :
017
正在转换为 15
037
正在转换为 31
还发现最后8 0r 9的数字保持相同
例如:
Also found that numbers having 8 0r 9 at end are remaining same for example :
018
18
038
38
o59
是 59
我发现的另一件事是
对于每个下一个10的范围,转换值和实际值之间的差异增加2
for each next range of 10 the difference between converted value and the actual value get incremented by 2
例如:
范围 00-09
差异是 0
即07的值将是7,04将是4
for range 00-09
difference is 0
i.e value of 07 will be 7, 04 will be 4
范围 010-019
差异是 2
017的值将是15,013将是11
for range 010-019
difference is 2
value of 017 will be 15, 013 will be 11
范围 020-029
差异是 4
027的值将是23,021将是17
for range 020-029
difference is 4
value of 027 will be 23, 021 will be 17
依旧..
这里是一个用于测试的snipet http://jsfiddle.net/rajubera/BxQHF/
here is a snipet for test http://jsfiddle.net/rajubera/BxQHF/
我不是得到为什么会发生这种情况?
I am not getting why this is happening ?
请帮助我如何从前导零的数字中得到正确的十进制数?
Please help me how to get the correct decimal number from the number having leading zero ?
推荐答案
如果有一个前导0,它将它转换为八进制(基数为8),只要它是基数为8的有效数字(没有大于7的数字)。
If there is a leading 0, it is converting it to octal (base 8) as long as its a valid number in base 8 (no numbers greater than 7).
例如:
017
基数为8 * 8 + 7 = 15
037
基数为8 * 3 + 8 + 7 = 31
017
in base 8 is 1 * 8 + 7 = 15
037
in base 8 is 3 * 8 + 7 = 31
018
转换为 18
,因为 018
不是基数为8的有效数字
018
is converted to 18
because 018
isn't a valid number in base 8
请注意,默认情况下,数字转换为哪个基数的行为可以是浏览器特定的,因此始终指定基数/的重要性使用 parseInt
:
Note that the behavior as to which base the number is converted to by default can be browser-specific, so its important to always specify the base/radix when using parseInt
:
parseInt(017,10)=== 17
UPDATE基于评论:
UPDATE based on comments:
parseInt
期望字符串
作为第一个参数,所以
parseInt
expects a string
as the first argument, so
parseInt(012,10)=== 12
这篇关于Javascript - 将数字转换为将数字转换为某个不同数字的数字。不明白为什么会这样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!