简体   繁体   English

如何从函数参数中的数字中删除前导零

[英]How to remove leading zeros from a number within a function parameter

I have a function that takes a number/integer as a parameter, and I'm trying to remove any possible leading zeros from that number before doing anything else in the function. 我有一个将数字/整数作为参数的函数,并且在执行该函数中的任何其他操作之前,我试图从该数字中删除任何可能的前导零。 However, numbers with leading zeros seem to be parsed in some way already before I can do something with the function parameter. 但是,在我可以对function参数执行某些操作之前,似乎已经以某种方式解析了带有前导零的数字。

So far I tried this: 到目前为止,我已经尝试过了:

function numToString(num){
    var string = num.toString();
    console.log(string);
};

numToString(0011); // output is "9"
numToString(1111); // output is "1111"

I also tried this: 我也试过这个:

function numToString(num, base){
    var string = num.toString();
    var parse = parseInt(string, base);
    console.log(parse);
};

numToString(0011, 10); // output is "9"
numToString(1111, 10); // output is "1111"

Of course, the second example doesn't work since num.toString() didn't give my expected outcome in the first place. 当然,第二个示例不起作用,因为num.toString()最初没有给出我的预期结果。 I don't want to replace the "num" function parameter with a string, but keep it as a number. 我不想将“ num”函数参数替换为字符串,而是将其保留为数字。

Maybe there is something obvious that I'm missing, but I can't figure quite out what it is. 也许我很想念某些东西,但是我无法弄清楚它到底是什么。 I am aware that a number with leading zeros is seen as an octal number, but I would like to know if I can work with the number that is entered as a parameter when it has leading zeros (ie it doesn't come from a variable, but is literally entered as a function parameter). 所知,有前导零的数被视为一个八进制数,但我想知道如果我可以当它已经领先的零输入为参数的数字工作(即它不是来自一个变量,但实际上是作为函数参数输入的)。

The reason this is happening is because leading a number with a zero makes javascript interpret the number in octal format. 发生这种情况的原因是,以零开头的数字会使javascript以八进制格式解释该数字。 There is no way that you can change this interpretation, but if you're getting the value from somewhere as a string you could use parseInt(string, 10) 您无法更改此解释,但是如果您从某个地方获取值作为字符串,则可以使用parseInt(string, 10)

We can do this string conversion ourselves and then parse it in base 10 like so: 我们可以自己进行此字符串转换,然后以10为底进行解析,如下所示:

 let number = 0011 console.log(parseInt(number.toString(8),10)) 

The other part of your question asks if you can throw when an octal number is entered. 问题的另一部分询问输入八进制数字时是否可以抛出。

'use strict' does just this: 'use strict'就是这样:

Sixth, a strict mode in ECMAScript 5 forbids octal syntax. 第六,ECMAScript 5中的严格模式禁止使用八进制语法。 The octal syntax isn't part of ECMAScript 5, but it's supported in all browsers by prefixing the octal number with a zero: 0644 === 420 and "\\045" === "%" . 八进制语法不是ECMAScript 5的一部分,但所有浏览器均支持八进制语法,方法是将八进制数字加零作为前缀: 0644 === 420"\\045" === "%" In ECMAScript 2015 Octal number is supported by prefixing a number with "0o" . 在ECMAScript 2015中,通过为数字加上前缀"0o"来支持八进制数字。 ie var a = 0o10; // ES2015: Octal var a = 0o10; // ES2015: Octal var a = 0o10; // ES2015: Octal

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Strict_mode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM