Javascript,为什么被视为八进制

发布于 2024-12-29 22:22:47 字数 1012 浏览 0 评论 0原文

我将 id 作为参数传递给 JavaScript 函数,因为它来自 UI,所以用零填充。但它似乎有(也许)“奇怪”的行为?

    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

我如何确保它们解析到正确的数字? (十进制)

编辑:

只是让它更清楚:

这些数字来自服务器并且是零填充的字符串。我正在为每个按钮制作一个删除按钮。

就像:

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
 }

我尝试过:

console.log(parseInt(0000022115, 10));  // 9293 which is 22115's octal

并且仍然解析为八进制

I'm passing as parameter an id to a javascript function, because it comes 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 come 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

娜些时光,永不杰束 2025-01-05 22:22:47

如果您将参数作为字符串对象接收,则应该可以将

 parseInt(string, 10)

字符串解释为十进制,即使它们以 0 开头。

在您的测试中,您向 parseInt 方法传递了一个数字,而不是字符串,也许这就是它不这样做的原因t 返回预期结果。

尝试

 parseInt('0000022115', 10)

而不是

parseInt(0000022115, 10)

为我返回 221115 。

If 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.

遗失的美好 2025-01-05 22:22:47

如果以 0 开头,它将被解释为八进制数。

http://www.hunlock.com/blogs/The_Complete_Javascript_Number_Reference#quickIDX2

请参阅 文章的警告在这里:

永远不要在数字前加零,除非您是
专门寻找八进制转换!

考虑在这里寻找删除前导 0 的想法:
在 Javascript 中截断字符串的前导零

If you start it with a 0, it's interpreted as an Octal number.

See http://www.hunlock.com/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

记忆で 2025-01-05 22:22:47

前导 0 表示该数字是八进制的。

parseInt 解析包含数字的字符串。
parseInt(0000022115, 10) 传递数字文字。该文字由 JS 解释器解析为八进制,因此您将原始数值传递给 parseInt

Leading 0s 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.

扭转时空 2025-01-05 22:22:47

除非你能截获这个数字的字符串版本,否则你就不走运了。

话虽如此,如果您可以获得八进制的字符串版本(调用 toString() 没有帮助),那么这将起作用:

parseInt(variable_string.replace(/^0+/, ''), 10);

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);
恬淡成诗 2025-01-05 22:22:47

尝试

/^[0]*([1-9]\d)/.exec(numberFromUI)[0]

一下应该会给你去掉零的数字(如果你必须支持小数,你需要编辑以解释“.”,当然“,”也很有趣......我真的希望你不必处理欧洲人写数字的所有疯狂的不同方式!)

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! )

潇烟暮雨 2025-01-05 22:22:47

如果数字来自服务器作为零填充字符串,则使用 +"0000022115"

console.log(+"0000022115")

if (021 < 019) console.log('Paradox');

JS 仅当它们是有效的八进制时才将零填充数字视为八进制 - 如果不是,则将其视为十进制。不允许悖论'use strict'模式

'use strict'

if (021 < 019) console.log('Paradox');

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');

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文