Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input field value - remove leading zeros

People also ask

How do you remove leading zeros in HTML?

Use the inbuilt replaceAll() method of the String class which accepts two parameters, a Regular Expression, and a Replacement String. To remove the leading zeros, pass a Regex as the first parameter and empty string as the second parameter. This method replaces the matched value with the given string.

How do you remove leading zeros from a field in ABAP?

SHIFT C LEFT DELETING LEADING '0'.


More simplified solution is as below. Check this out!

var resultString = document.getElementById("theTextBoxInQuestion")
                           .value
                           .replace(/^[0]+/g,"");

str.replace(/^0+(?!\.|$)/, '')

  '0000.00' --> '0.00'   
     '0.00' --> '0.00'   
  '00123.0' --> '123.0'   
        '0' --> '0'  

var value= document.getElementById("theTextBoxInQuestion").value;
var number= parseFloat(value).toFixed(2);

It sounds like you just want to remove leading zeros unless there's only one left ("0" for an integer or "0.xxx" for a float, where x can be anything).

This should be good for a first cut:

while (s.charAt(0) == '0') {            # Assume we remove all leading zeros
    if (s.length == 1) { break };       # But not final one.
    if (s.charAt(1) == '.') { break };  # Nor one followed by '.'
    s = s.substr(1, s.length-1)
}

Ok a simple solution. The only problem is when the string is "0000.00" result in plain 0. But beside that I think is a cool solution.

var i = "0000.12";
var integer = i*1; //here's is the trick...
console.log(i); //0000.12
console.log(integer);//0.12

For some cases I think this can work...