Javascript examples for Number Operation:Number Format
Pad a number with leading zeros
<html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <script type="text/javascript"> window.onload=( function() {//from w w w . j a v a 2 s .c o m function pad(input) { var BASE = "0000"; return input ? BASE .substr(0, 4 - Math.ceil(input / 10)) + input : BASE; } console.log(pad(0)); console.log(pad(9999)); }); </script> </head> <body> </body> </html>