DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

LeftZeroPad

04.08.2005
| 2779 views |
  • submit to reddit
        
var MANY_ZEROS = "000000000000000000";

function leftZeroPad(val, minLength) {
    if (typeof(val) != "string")
        val = String(val);
    return (MANY_ZEROS.substring(0, minLength - val.length)) + val;
}

leftZeroPad(3, 2) ==> "03"