All TalkersCode Topics

Last Updated : Mar 11, 2024

In this article we will show you the solution of JavaScript format number leading zero, if we format a number in JavaScript without leading zero.

We can use several methods to format a number. We use slice() and concat() method, template literals and padstart() method and many more methods to format a leading number zero in javascript.

Step By Step Guide On JavaScript Format Number Leading Zero :-

There are several methods to format a number leading zero in javascript.

Method 1 - slice() and concat() method to format a number leading zero in javascript

```const num= 3;
const formatnum=”0” + num.toString().slice(-1);
console.log(formatnum);
// output is “03”
```
1. Firstly, we can define a constant variable named num and the value of num is 3.
2. Then we use num.toString() method this will convert the num into string
3. In this code,we can use slice(-1) to extracting the end character from the string.
4. After that concatening the string using concat(‘+’) method, We can concatenate “0” with the extracted character “3”. “03” concatenated string assign to formatnum variable
5. Hence, the output is “03”

Method 2 - toLocaleString() method with minimumIntegerDigits method to format a number leading zero in javascript

```const number= 4;
const formatnum= number.toLocaleString(undefined,{minimumIntegerDigits:3});
console.log(formatnum);
```
1. Firstly, we can declare constant variable named “number” and the value of number is 4.
2. Secondly, we can declare constant variable named formatnum.
3. In formatnum, we can use number.toLocalestring() method to convert the number into localized string format
4. In number. toLocaleString() method we can passed “undefined” as a first argument
5. Another we can use minimumIntegerDigits , it ensures the output string contain atleast 3 digits before decimal separator.
6. Then, console.log print the output of this code: “004”

Method 3 - String() and padStart() methods to format a number leading zero in javascript

```const number=4;
console.log(formatnum);
```
1. Firstly, we can declare constant variable named number and the value of number is 4.
2. Secondly, we can declare another constant variable named formatnum
3. In formatnum, we can use String(number) to convert the number into string.
4. padStart() method is used for adding a padding characters to the beginning of string, the first argument that we pass in padStart() is the targeted length of string and other is the string of characters used for padding . In this code we can use padStart(3,’0’) which means 3 is the targeted length of string and ‘0’ is the string of characters used for padding
5. output of this code:- “004”

Conclusion :-

In conclusion, there are lots of method of formatting number leading zero in javascript.

We use padStart() method, toLocaleString() method, slice() method, concat() method.

In padStart() method, we can pad a string with character. In toLocaleString() method, it formatting a string with leading Zero.

In slice() method, we can extracting a end character of the string.

I hope this article on JavaScript format number leading zero helps you and the steps and method mentioned above are easy to follow and implement.