. Advertisement .
..3..
. Advertisement .
..4..
Are you a beginning thinking to know how to pad a number with leading zeros in JavaScript? Well, it is one of the JavaScript projects that you must be wondering how to crack. JavaScript is versatile with infinite numbers of projects that can help beginners as well as give an opportunity to the programmers to add as many elements to their website as they want to make it interactive for the users. From making a website for target users to asking them to input the values to get the result, JavaScript can do everything. Adding a number with the leading zeros is a program that is our today’s topic of discussion.
Frame numbers with specified numbers of digits, where unused digits are filled with zeros, and this is known as padded numbers. Let’s check out how to pad a number with leading zeros
How to Pad a number with Leading Zeros in JavaScript
To pad a number, we use the padStart()
method which is a built-in method of JavaScript. In this method, the string is padded or concatenated with another string by the time the string returns the specified length. We apply the padding from the start that continues until we reach the specified length and along with the padstring.
Let’s go through the syntax that can be used
Using padStart() Method
Syntax
padStart(targetLength)
padStart(targetLength, padString)
As we can see, it accepts two parameters; the target length and pad string. The target length is used for the string that can go up to the length. And, the padded string is used to pad the string. It also accepts the minus sign or decimal point. On the other hand, pad string is known as the substring that we apply at the start.
Check out the code:
function padNumberZero(string1, total_length_of_string) {
return String(string1).padStart(total_length_of_string, '0');
}
console.log(padNumberZero(7, 2));
console.log(padNumberZero(7, 3));
console.log(padNumberZero(7, 4));
console.log(padNumberZero('A', 2));
console.log(padNumberZero('AB', 3));
console.log(padNumberZero("ABC", 4));
Output:
07
007
0007
0A
0AB
0ABC
Try another way:
// For number
const number1 = '000' + 1985;
console.log(number1);
// For text
const text1 = '000' + "aBc";
console.log(text1);
Output
// For number
0001985
// For text
000aBc
When you convert the padded string to the number back again, all the leading zeros are removed automatically as 008 => 8. This happens because JavaScript doesn’t hold leading zeros.
Deal with Negative numbers
If the input string length given exceeds the target length that is specified, we get the padstart method in return by the entire string. Dealing with negative numbers requires you to add an ‘if’ statement that adds the minus sign once the leading zeros are added. See the code
function padNumberZero1(string2, total_length_of_string) {
if (string2 < 0) {
const withoutNegativeSign = String(string2).slice(1);
return '-' + withoutNegativeSign.padStart(total_length_of_string, '0');
}
return String(string2).padStart(total_length_of_string, '0');
}
console.log(padNumberZero1(7, 3));
console.log(padNumberZero1(-77, 4));
console.log(padNumberZero1('s',5));
Output
007
-0077
0000s
Conclusion
We covered how to pad a number with leading zeros in JavaScript. The padStart is the method that is used to do the task. With this, I wish you loads of luck!
Hope your code well! Your comments below are always entertained so feel free to drop a message.
Leave a comment