JavaScript Format Number Leading Zero
Last Updated : Mar 11, 2024
IN - JavaScript | Written & Updated By - Dikshita
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”
- Firstly, we can define a constant variable named num and the value of num is 3.
- Then we use num.toString() method this will convert the num into string
- In this code,we can use slice(-1) to extracting the end character from the string.
- After that concatening the string using concat(‘+’) method, We can concatenate “0” with the extracted character “3”. “03” concatenated string assign to formatnum variable
- 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);
- Firstly, we can declare constant variable named “number” and the value of number is 4.
- Secondly, we can declare constant variable named formatnum.
- In formatnum, we can use number.toLocalestring() method to convert the number into localized string format
- In number. toLocaleString() method we can passed “undefined” as a first argument
- Another we can use minimumIntegerDigits , it ensures the output string contain atleast 3 digits before decimal separator.
- 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; const formatnum= String(number).padStart(3,’0’); console.log(formatnum);
- Firstly, we can declare constant variable named number and the value of number is 4.
- Secondly, we can declare another constant variable named formatnum
- In formatnum, we can use String(number) to convert the number into string.
- 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
- 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.