

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to pad a number with leading zeros in JavaScript?
To pad a number with leading zeros, a function is created here, which checks the number with its length and add leading zeros.
Example
You can try to run the following code to pad a number with leading zeros in JavaScript −
<!DOCTYPE html> <html> <body> <script> String.prototype.padFunction = function(padStr, len) { var str = this; while (str.length < len) str = padStr + str; return str; } var str = "2"; document.write(str.padFunction("0", 4)); var str = "8"; document.write("<br>"+str.padFunction("0", 5)); </script> </body> </html>
Output
0002 00008
- Related Questions & Answers
- How to remove leading zeros from a number in JavaScript?
- Remove leading zeros in a JavaScript array?
- Left pad a String in Java with zeros
- Java Program to add leading zeros to a number
- Remove leading zeros in array - JavaScript
- Remove leading zeros in array in JavaScript
- Left pad an integer in Java with zeros
- Add leading zeros to a MySQL column?
- Print leading zeros with C++ output operator
- How to convert a string with zeros to number in JavaScript?
- C Program to count trailing and leading zeros in a binary number
- Number of leading zeros in binary representation of a given number in C++
- Add leading Zeros to Python string
- Java Program to Remove leading zeros
- Pad the values of the column with zeros in MySQL
Advertisements