Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

LeftZeroPad

DZone's Guide to

LeftZeroPad

·
Free Resource

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"
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}