Javascript 从Javascript中的数字中删除前导零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6676488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove leading zeros from a number in Javascript
提问by testndtv
Possible Duplicate:
Truncate leading zeros of a string in Javascript
可能的重复:
在 Javascript 中截断字符串的前导零
What is the simplest and cross-browser compatible way to remove leading zeros from a number in Javascript ?
在 Javascript 中从数字中删除前导零的最简单且跨浏览器兼容的方法是什么?
e.g. If I have a textbox value as 014 or 065, it should only return 14 or 65
例如,如果我的文本框值为 014 或 065,则它应该只返回 14 或 65
回答by naveen
We can use four methods for this conversion
我们可以使用四种方法进行这种转换
- parseIntwith radix
10
- Number Constructor
- Unary Plus Operator
- Using mathematical functions (subtraction)
const numString = "065";
//parseInt with radix=10
let number = parseInt(numString, 10);
console.log(number);
// Number constructor
number = Number(numString);
console.log(number);
// unary plus operator
number = +numString;
console.log(number);
// conversion using mathematical function (subtraction)
number = numString - 0;
console.log(number);
Update(based on comments): Why doesn't this work on "large numbers"?
更新(基于评论):为什么这不适用于“大数字”?
For the primitive type Number
, the safest max value is 253-1(Number.MAX_SAFE_INTEGER
).
对于原始类型Number
,最安全的最大值是2 53-1( Number.MAX_SAFE_INTEGER
)。
console.log(Number.MAX_SAFE_INTEGER);
Now, lets consider the number string '099999999999999999999'and try to convert it using the above methods
现在,让我们考虑数字字符串'099999999999999999999'并尝试使用上述方法对其进行转换
const numString = '099999999999999999999';
let parsedNumber = parseInt(numString, 10);
console.log(`parseInt(radix=10) result: ${parsedNumber}`);
parsedNumber = Number(numString);
console.log(`Number conversion result: ${parsedNumber}`);
parsedNumber = +numString;
console.log(`Appending Unary plus operator result: ${parsedNumber}`);
parsedNumber = numString - 0;
console.log(`Subtracting zero conversion result: ${parsedNumber}`);
All results will be incorrect.
所有的结果都将是不正确的。
That's because, when converted, the numString value is greater than Number.MAX_SAFE_INTEGER
. i.e.,
这是因为在转换时, numString 值大于Number.MAX_SAFE_INTEGER
。IE,
99999999999999999999 > 9007199254740991
This means all operation performed with the assumption that the string
can be converted to number
type fails.
这意味着在假设string
可以转换为number
类型的情况下执行的所有操作都失败了。
For numbers greater than 253, primitive BigInt
has been added recently. Check browser compatibility of BigInt
here.
对于大于2 53 的数字,BigInt
最近添加了原语。检查BigInt
此处的浏览器兼容性。
The conversion code will be like this.
转换代码将是这样的。
const numString = '099999999999999999999';
const number = BigInt(numString);
P.S: Why radix is important for parseInt
?
PS:为什么基数对 很重要parseInt
?
If radix is undefined or 0 (or absent), JavaScript assumes the following:
如果 radix 未定义或为 0(或不存在),则 JavaScript 假定如下:
- If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed
- If the input string begins with "0", radix is eight (octal) or 10 (decimal)
- If the input string begins with any other value, the radix is 10 (decimal)
- 如果输入字符串以“0x”或“0X”开头,则基数为 16(十六进制)并解析字符串的其余部分
- 如果输入字符串以“0”开头,则基数为八(八进制)或 10(十进制)
- 如果输入字符串以任何其他值开头,则基数为 10(十进制)
Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet.
究竟选择哪个基数取决于实现。ECMAScript 5 指定使用 10(十进制),但并非所有浏览器都支持它。
For this reason, always specify a radix when using parseInt
为此,在使用 parseInt 时总是指定一个基数
回答by keymone
regexp:
正则表达式:
"014".replace(/^0+/, '')
回答by Felix Kling
It is not clear why you want to do this. If you want to get the correct numerical value, you could use unary +
[docs]:
目前尚不清楚您为什么要这样做。如果你想获得正确的数值,你可以使用一元+
[docs]:
value = +value;
If you just want to format the text, then regex could be better. It depends on the values you are dealing with I'd say. If you only have integers, then
如果您只想格式化文本,那么正则表达式可能会更好。这取决于我要说的您正在处理的价值观。如果你只有整数,那么
input.value = +input.value;
is fine as well. Of course it also works for float values, but depending on how many digits you have after the point, converting it to a number and back to a string could (at least for displaying) remove some.
也很好。当然,它也适用于浮点值,但取决于点后有多少位数字,将其转换为数字并返回到字符串可以(至少用于显示)删除一些。