通过 javascript/jquery 删除/截断前导零
建议通过 javascript、jquery 从数字(任何字符串)中删除或截断前导零的解决方案。
Suggest solution for removing or truncating leading zeros from number(any string) by javascript,jquery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
您可以使用匹配字符串开头的零的正则表达式:
You can use a regular expression that matches zeroes at the beginning of the string:
我会使用 Number() 函数:
或者我会将字符串乘以 1
I would use the Number() function:
Or I would multiply my string by 1
也许有点晚了,但我想加我的 2 美分。
如果您的字符串始终表示一个数字,并且可能有前导零,则可以使用“+”运算符将字符串简单地转换为数字。
例如,
如果您的字符串不代表数字,并且您只需要删除 0,请使用其他解决方案,但如果您只需要它们作为数字,这是最短的方法。
仅供参考,您可以做相反的事情,如果您将空字符串连接到数字,则强制数字充当字符串,例如:
希望它可以帮助某人
Maybe a little late, but I want to add my 2 cents.
if your string ALWAYS represents a number, with possible leading zeros, you can simply cast the string to a number by using the '+' operator.
e.g.
if your string doesn't represent a number and you only need to remove the 0's use the other solutions, but if you only need them as number, this is the shortest way.
and FYI you can do the opposite, force numbers to act as strings if you concatenate an empty string to them, like:
hope it helps somebody
既然你说“任何字符串”,我假设这也是你想要处理的字符串。
因此,正则表达式是正确的方法:
这将防止丢失“000000”值。
您可以在此处查看工作示例
Since you said "any string", I'm assuming this is a string you want to handle, too.
So, regex is the way to go:
And this will prevent loss of a "000000" value.
You can see a working example here
这会很好地工作。
This will work nicely.
我得到了这个在 javascript 中截断前导零(数字或任何字符串)的解决方案:
I got this solution for truncating leading zeros(number or any string) in javascript:
只需尝试乘以一,如下所示:
Simply try to multiply by one as following:
1. 最明确的是使用 parseInt():
2. 另一种方法是使用 + 一元运算符:
3. 您还可以走正则表达式路线,如下所示:
1. The most explicit is to use parseInt():
2. Another way is to use the + unary operator:
3. You can also go the regular expression route, like this:
试试这个,
更多这里
Try this,
More here
您应该使用“parseInt”函数的“radix”参数:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FparseInt
parseInt('015', 10) => 15
如果你不使用它,某些 javascript 引擎可能会将它用作八进制
parseInt('015') =>; 0
You should use the "radix" parameter of the "parseInt" function :
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FparseInt
parseInt('015', 10) => 15
if you don't use it, some javascript engine might use it as an octal
parseInt('015') => 0
如果数字是 int 使用
如果数字是 float 使用
If number is int use
If the number is float use
常量数 = '0000007457841';
console.log(+number) //7457841;
OR number.replace(/^0+/, '')
const number = '0000007457841';
console.log(+number) //7457841;
OR number.replace(/^0+/, '')
来自 Guffa 的正则表达式解决方案,但至少保留一个字符
Regex solution from Guffa, but leaving at least one character
使用“Math.abs”,
例如:Math.abs(003) = 3;
Use "Math.abs"
eg: Math.abs(003) = 3;
我想删除字符串中每个数字序列的所有前导零,并在数字值等于零时返回 0。
我最终这样做了:
I wanted to remove all leading zeros for every sequence of digits in a string and to return 0 if the digit value equals to zero.
And I ended up doing so:
另一种没有正则表达式的方法:
使用短字符串,它会比正则表达式更快(jsperf)
One another way without regex:
With short string it will be more faster than regex (jsperf)
现在,您可以从任何字符串中删除所有前面的 0,而无需删除该字符串内部的 0
Now here you can remove all the front 0 from any string without removing 0's inside that string