I have a textbox in Javascript. When I enter '0000.00'
in the textbox, I want to know how to convert that to only having one leading zero, such as '0.00'
.

- 76,741
- 107
- 159
- 260
-
Need to clarify what a "string" is. If your "string" is an decimal number, then the answer will be different than if your "string" is a text string. "00000.000" -> "0.000" would be a decimal conversion. "000TXT10001" -> "0TXT10001" would require a trim function. For instance what are you expecting when the input is "0011.1100" -> "011.1100" or "11.11"? – ChronoFish Oct 15 '12 at 17:28
8 Answers
More simplified solution is as below. Check this out!
var resultString = document.getElementById("theTextBoxInQuestion")
.value
.replace(/^[0]+/g,"");

- 48,120
- 14
- 91
- 142
str.replace(/^0+(?!\.|$)/, '')
'0000.00' --> '0.00'
'0.00' --> '0.00'
'00123.0' --> '123.0'
'0' --> '0'

- 13,156
- 2
- 34
- 47
-
-
@MikeM Could someone please elaborate? From my understanding of regexes and as of regexr.com/4tn79 the used regex does not match the zero in the 3rd example, but the function does indeed work for all the examples (using Chrome). So how does the 3rd example work if the regex does not match correctly?! And how can I be sure it will always work? – jneuendorf Feb 06 '20 at 02:35
var value= document.getElementById("theTextBoxInQuestion").value;
var number= parseFloat(value).toFixed(2);

- 4,736
- 2
- 21
- 24
-
4Does this cause any issues if the number of zeros to the left of the decimal is very large? – Eric Dec 01 '10 at 23:16
-
Eric's got a point, it will only work if you have one leading zero. Any more than that and fail. – Julian Young Sep 29 '11 at 14:55
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)
}

- 854,327
- 234
- 1,573
- 1,953
-
This is a good solution. Relying on built-in functions is not always the best option. I would turn this into a helper function that takes a string argument and returns the formatted string. – Kaleb Anderson Jan 20 '16 at 22:03
-
Works very well for me. I would just advise to add a piece of code to support negative numbers (with "-" in first character). – Gaston Nov 28 '17 at 10:17
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...

- 8,169
- 9
- 54
- 72
You can use this code:
<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>

- 55,015
- 38
- 216
- 226
You can use a regex to replace the leading zeroes with a single one:
valueString.replace(/^(-)?0+(0\.|\d)/, '$1$2')
>'-000.0050'.replace(/^(-)?0+(0\.|\d)/, '$1$2')
< "-0.0050"
>'-0010050'.replace(/^(-)?0+(0\.|\d)/, '$1$2')
< "-10050"
Matches: <beginning of text><optional minus sign><any sequence of zeroes><either a zero before the dot or another digit>
Replaces with: <same sign if available><the part of the string after the sequence of zeroes>
^ is the beginning of text
? means optional (refers to the previous character)
(a|b) means either a or b
. is the dot (escaped as . has a special meaning)
\d is any digit
$1 means what you found in the first set of ()
$2 means what you found in the second set of ()

- 1,815
- 2
- 29
- 63
-
3Can you please elaborate. Also is it possible not to post pictures ? Image links can break while text will always be there. – DarkMukke Oct 17 '18 at 14:51
-
Stackoverflow guidelines state to not post pics of text, code, data, or error messages. That including text output for code. Many reasons why: pics of text can be difficult to read, especially on mobile devices, it can cause unnecessary data download for metered cellular data, it cannot be copy-pasted or searched, and it is not accessibility friendly. Good uses of pics are when the issue describes a graphical issue that cannot be described in words. Please delete the unnecessary pic to meet SO guidelines. – SherylHohman Nov 01 '21 at 02:30
Try this:
<input type="text" onblur="this.value=this.value.replace(/^0+(?=\d\.)/, '')">

- 643,351
- 109
- 780
- 844