admin管理员组文章数量:1287577
A leading zero to some number converting the number to some unknown number format. for example :
017
is getting converted to 15
037
is getting converted to 31
Also found that numbers having 8 0r 9 at end are remaining same for example :
018
is 18
038
is 38
o59
is 59
one more thing that I found is
for each next range of 10 the difference between converted value and the actual value get incremented by 2
for example :
for range 00-09
difference is 0
i.e value of 07 will be 7, 04 will be 4
for range 010-019
difference is 2
value of 017 will be 15, 013 will be 11
for range 020-029
difference is 4
value of 027 will be 23, 021 will be 17
and so on..
here is a snipet for test /
I am not getting why this is happening ?
Please help me how to get the correct decimal number from the number having leading zero ?
A leading zero to some number converting the number to some unknown number format. for example :
017
is getting converted to 15
037
is getting converted to 31
Also found that numbers having 8 0r 9 at end are remaining same for example :
018
is 18
038
is 38
o59
is 59
one more thing that I found is
for each next range of 10 the difference between converted value and the actual value get incremented by 2
for example :
for range 00-09
difference is 0
i.e value of 07 will be 7, 04 will be 4
for range 010-019
difference is 2
value of 017 will be 15, 013 will be 11
for range 020-029
difference is 4
value of 027 will be 23, 021 will be 17
and so on..
here is a snipet for test http://jsfiddle/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 ?
Share Improve this question edited Jul 31, 2013 at 14:53 Raju Bera asked Jul 31, 2013 at 14:26 Raju BeraRaju Bera 1,0189 silver badges14 bronze badges 5- 4 You're going to need to post some code so we can reproduce the issue. – Jason P Commented Jul 31, 2013 at 14:29
- Possible duplicate of Leading zero in javascript and Javascript, why treated as octal – apsillers Commented Jul 31, 2013 at 14:44
- possible duplicate of Javascript parseInt() with leading zeros – Bergi Commented Jul 31, 2013 at 14:44
- no in my case parseInt(012,10) doesn't help. you can check the jsfiddle link – Raju Bera Commented Jul 31, 2013 at 14:56
-
2
Why are you writing your numbers with leading zeros? If you want decimal values, just don't do that. If you are getting the values as string input,
parseInt(string,10)
will work (but you are correct that usingparseInt
on an octal literal will not convert it to decimal. – apsillers Commented Jul 31, 2013 at 15:05
2 Answers
Reset to default 10If 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).
For example:
017
in base 8 is 1 * 8 + 7 = 15
037
in base 8 is 3 * 8 + 7 = 31
018
is converted to 18
because 018
isn't a valid number in base 8
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 based on ments:
parseInt
expects a string
as the first argument, so
parseInt("012",10) === 12
One of the reasons to "use strict";
(function() {"use strict"; 017})()
// Firefox => SyntaxError: "0"-prefixed octal literals and octal escape sequences are deprecated; for octal literals use the \"0o\" prefix instead
// Chrome, Node => SyntaxError: Octal literals are not allowed in strict mode.
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_octal
本文标签:
版权声明:本文标题:jquery - Javascript - Leading zero to a number converting the number to some different number. not getting why this happening? - 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741267807a2368812.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论