Remove/ truncate leading zeros by javascript/jquery

JavascriptJqueryNumbersZero

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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

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

Solution 2 - Javascript

I would use the Number() function:

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

Or I would multiply my string by 1

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

Solution 3 - Javascript

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.

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.

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

Solution 4 - Javascript

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.

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

You can see a working example here

Solution 5 - Javascript

parseInt(value) or parseFloat(value)

This will work nicely.

Solution 6 - Javascript

I got this solution for truncating leading zeros(number or any string) in 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>

Solution 7 - Javascript

Simply try to multiply by one as following:

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

Solution 8 - Javascript

Try this,

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

More here

Solution 9 - Javascript

1. The most explicit is to use parseInt():

parseInt(number, 10)

2. Another way is to use the + unary operator:

+number

3. You can also go the regular expression route, like this:

number.replace(/^0+/, '')

Solution 10 - Javascript

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

Solution 11 - Javascript

If number is int use

"" + parseInt(str)

If the number is float use

"" + parseFloat(str)

Solution 12 - Javascript

const number = '0000007457841'; console.log(+number) //7457841;

OR number.replace(/^0+/, '')

Solution 13 - Javascript

Regex solution from Guffa, but leaving at least one character

"123".replace(/^0*(.+)/, '$1'); // 123
"012".replace(/^0*(.+)/, '$1'); // 12
"000".replace(/^0*(.+)/, '$1'); // 0

Solution 14 - Javascript

One another way without regex:

function trimLeadingZerosSubstr(str) {
    var xLastChr = str.length - 1, xChrIdx = 0;
    while (str[xChrIdx] === "0" && xChrIdx < xLastChr) {
        xChrIdx++;
    }
    return xChrIdx > 0 ? str.substr(xChrIdx) : str;
}

With short string it will be more faster than regex (jsperf)

Solution 15 - Javascript

const input = '0093';
const match = input.match(/^(0+)(\d+)$/);
const result = match && match[2] || input;

Solution 16 - Javascript

Use "Math.abs"

eg: Math.abs(003) = 3;

console.log(Math.abs(003))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSomnath MulukView Question on Stackoverflow
Solution 1 - JavascriptGuffaView Answer on Stackoverflow
Solution 2 - JavascriptJade HamelView Answer on Stackoverflow
Solution 3 - JavascriptDiegoDDView Answer on Stackoverflow
Solution 4 - JavascriptJohn FisherView Answer on Stackoverflow
Solution 5 - JavascriptKrupallView Answer on Stackoverflow
Solution 6 - JavascriptSomnath MulukView Answer on Stackoverflow
Solution 7 - JavascriptSiyavash HamdiView Answer on Stackoverflow
Solution 8 - JavascriptGowriView Answer on Stackoverflow
Solution 9 - JavascriptSurya R PraveenView Answer on Stackoverflow
Solution 10 - JavascriptantoineView Answer on Stackoverflow
Solution 11 - Javascriptloakesh bachhuView Answer on Stackoverflow
Solution 12 - JavascriptVivek KapoorView Answer on Stackoverflow
Solution 13 - JavascriptJoostView Answer on Stackoverflow
Solution 14 - JavascriptmadreasonView Answer on Stackoverflow
Solution 15 - JavascriptBoneyView Answer on Stackoverflow
Solution 16 - JavascriptKorah Babu VargheseView Answer on Stackoverflow