admin管理员组文章数量:1387276
I'm passing as parameter an id to a javascript function, because it es from UI, it's left zero padded. but it seems to have (maybe) "strange" behaviour?
console.log(0000020948); //20948
console.log(0000022115); //9293 which is 22115's octal
console.log(parseInt(0000022115, 10)); // 9293 which is 22115's octal
console.log(0000033959); //33959
console.log(20948); //20948
console.log(22115); //22115
console.log(33959); //33959
how can I make sure they are parsing to right numebr they are? (decimal)
EDIT:
just make it clearer:
those numbers e from the server and are zero padded strings. and I'm making a delete button for each one.
like:
function printDelButton(value){
console.log(typeof value); //output string
return '<a href="#" onclick="deleteme('+value+')"><img src="images/del.png"></a>'
}
and
function printDelButton(value){
console.log(typeof value); //output numeric
console.log(value); //here output as octal .... :S
}
I tried :
console.log(parseInt(0000022115, 10)); // 9293 which is 22115's octal
and still parsing as Octal
I'm passing as parameter an id to a javascript function, because it es from UI, it's left zero padded. but it seems to have (maybe) "strange" behaviour?
console.log(0000020948); //20948
console.log(0000022115); //9293 which is 22115's octal
console.log(parseInt(0000022115, 10)); // 9293 which is 22115's octal
console.log(0000033959); //33959
console.log(20948); //20948
console.log(22115); //22115
console.log(33959); //33959
how can I make sure they are parsing to right numebr they are? (decimal)
EDIT:
just make it clearer:
those numbers e from the server and are zero padded strings. and I'm making a delete button for each one.
like:
function printDelButton(value){
console.log(typeof value); //output string
return '<a href="#" onclick="deleteme('+value+')"><img src="images/del.png"></a>'
}
and
function printDelButton(value){
console.log(typeof value); //output numeric
console.log(value); //here output as octal .... :S
}
I tried :
console.log(parseInt(0000022115, 10)); // 9293 which is 22115's octal
and still parsing as Octal
Share Improve this question edited May 2, 2019 at 11:56 Kamil Kiełczewski 92.9k34 gold badges395 silver badges370 bronze badges asked Jan 30, 2012 at 22:21 YichzYichz 9,68110 gold badges60 silver badges98 bronze badges 7- Instead of padding your numbers using zeros, use spaces. – Rob W Commented Jan 30, 2012 at 22:25
- Why does the UI left pad it with 0s? – jglouie Commented Jan 30, 2012 at 22:26
-
It may be a good idea to use a
"use strict"
directive in your script. That will cause any use of octal literals to throw an error and it should make things easier to debug! – James Allardice Commented Jan 30, 2012 at 22:27 -
3
Regarding
parseInt(0000022115, 10)
: You are still passing a number as first parameter, which makes JavaScript convert the number first and then pass it toparseInt
. Pass a string instead. – Felix Kling Commented Jan 30, 2012 at 22:46 -
1
If you're creating a new element from the
'<a...</a>'
string, then you need to wrap thevalue
part of the string in quotes.'<a href="#" onclick="deleteme(\''+value+'\')">'
– user1106925 Commented Jan 30, 2012 at 22:53
6 Answers
Reset to default 5If you receive your parameters as string objects, it should work to use
parseInt(string, 10)
to interpret strings as decimal, even if they are beginning with 0.
In your test, you pass the parseInt method a number, not a string, maybe that's why it doesn't return the expected result.
Try
parseInt('0000022115', 10)
instead of
parseInt(0000022115, 10)
that does return 221115 for me.
If you start it with a 0, it's interpreted as an Octal number.
See http://www.hunlock./blogs/The_Complete_Javascript_Number_Reference#quickIDX2
Note the article's warning here:
You should never precede a number with a zero unless you are specifically looking for an octal conversion!
Consider looking here for ideas on removing the leadings 0s: Truncate leading zeros of a string in Javascript
Leading 0
s indicate that the number is octal.
parseInt
parses a string containing a number.
parseInt(0000022115, 10)
passes a numeric literal. The literal is parsed in octal by the JS interpreter, so you're passing a raw numeric value to parseInt
.
Unless you can intercept a string version of this number, you're out of luck.
That being said, if you can get a string version of your octal (calling toString()
won't help), this will work:
parseInt(variable_string.replace(/^0+/, ''), 10);
Try
/^[0]*([1-9]\d)/.exec(numberFromUI)[0]
That should give you just the numbers stripping the zeros (if you have to support decimals, you'll need to edit to account for the '.', and of course ',' is fun too... and I really hope you don't have to handle all the crazy different ways Europeans write numbers! )
If number came from server as zero padded string then use +"0000022115"
console.log(+"0000022115")
if (021 < 019) console.log('Paradox');
JS treat zero padded numbers like octal only if they are valid octal - if not then it treat it as decimal. To not allow paradox 'use strict'
mode
'use strict'
if (021 < 019) console.log('Paradox');
本文标签: javascriptwhy treated as octalStack Overflow
版权声明:本文标题:Javascript, why treated as octal - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744194004a2594650.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论