通过 javascript/jquery 删除/截断前导零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8276451/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:24:14  来源:igfitidea点击:

Remove/ truncate leading zeros by javascript/jquery

javascriptjquerynumberszero

提问by Somnath Muluk

Suggest solution for removing or truncating leading zeros from number(any string) by javascript,jquery.

建议通过 javascript,jquery 从数字(任何字符串)中删除或截断前导零的解决方案。

回答by Guffa

You can use a regular expression that matches zeroes at the beginning of the string:

您可以使用匹配字符串开头的零的正则表达式:

s = s.replace(/^0+/, '');

回答by Charles Hamel

I would use the Number() function:

我会使用 Number() 函数:

var str = "00001";
str = Number(str).toString();
>> "1"

Or I would multiply my string by 1

或者我将我的字符串乘以 1

var str = "00000000002346301625363";
str = (str * 1).toString();
>> "2346301625363"

回答by John Fisher

Since you said "any string", I'm assuming this is a string you want to handle, too.

既然你说“任何字符串”,我假设这也是你想要处理的字符串。

"00012  34 0000432    0035"

So, regex is the way to go:

所以,正则表达式是要走的路:

var trimmed = s.replace(/\b0+/g, "");

And this will prevent loss of a "000000" value.

这将防止丢失“000000”值。

var trimmed = s.replace(/\b(0(?!\b))+/g, "")

You can see a working example here

你可以在这里看到一个工作示例

回答by DiegoDD

Maybe a little late, but I want to add my 2 cents.

也许有点晚了,但我想加上我的 2 美分。

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.

例如

x= "00005";
alert(typeof x); //"string"
alert(x);// "00005"

x = +x ; //or x= +"00005"; //do NOT confuse with x+=x, which will only concatenate the value
alert(typeof x); //number , voila!
alert(x); // 5 (as number)

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.

如果您的字符串不代表数字并且您只需要删除 0,请使用其他解决方案,但如果您只需要它们作为数字,这是最短的方法。

and FYI you can do the opposite, force numbers to act as strings if you concatenate an empty string to them, like:

仅供参考,您可以做相反的事情,如果将空字符串连接到数字,则强制数字充当字符串,例如:

x = 5;
alert(typeof x); //number
x = x+"";
alert(typeof x); //string

hope it helps somebody

希望它可以帮助某人

回答by Krupall

parseInt(value) or parseFloat(value)

This will work nicely.

这将很好地工作。

回答by Somnath Muluk

I got this solution for truncating leading zeros(number or any string) in javascript:

我得到了在 javascript 中截断前导零(数字或任何字符串)的解决方案:

<script language="JavaScript" type="text/javascript">
<!--
function trimNumber(s) {
  while (s.substr(0,1) == '0' && s.length>1) { s = s.substr(1,9999); }
  return s;
}

var s1 = '00123';
var s2 = '000assa';
var s3 = 'assa34300';
var s4 = 'ssa';
var s5 = '121212000';

alert(s1 + '=' + trimNumber(s1));
alert(s2 + '=' + trimNumber(s2));
alert(s3 + '=' + trimNumber(s3));
alert(s4 + '=' + trimNumber(s4));
alert(s5 + '=' + trimNumber(s5));
// end hiding contents -->
</script>

回答by Gowri

Try this,

尝试这个,

   function ltrim(str, chars) {
        chars = chars || "\s";
        return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
    }

    var str =ltrim("01545878","0");

More here

更多在这里

回答by Siyavash Hamdi

Simply try to multiply by one as following:

简单地尝试乘以一如下:

"00123" * 1;              // Get as number
"00123" * 1 + "";       // Get as string

"00123" * 1; // 获取数字
"00123" * 1 + ""; // 获取字符串

回答by antoine

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”函数的“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

parseInt('015', 10) => 15

if you don't use it, some javascript engine might use it as an octal parseInt('015') => 0

如果您不使用它,某些 javascript 引擎可能会将其用作八进制 parseInt('015') => 0

回答by loki_dev

If number is int use

如果 number 是 int 使用

"" + parseInt(str)

If the number is float use

如果数字是浮动使用

"" + parseFloat(str)