Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q183 How can I format a number, e.g. 2, as 02 and 00002?

You are here: irt.org | FAQ | JavaScript | Number | Q183 [ previous next ]

To format 2 as 02 I use the following JavaScript function:

<script language="JavaScript"><!--
function padout(number) { return (number < 10) ? '0' + number : number; }

alert(padout(2));
//--></script>

To format 2 as 00002 I would recommend something like:

<script language="JavaScript"><!--
function pad(number,length) {
    var str = '' + number;
    while (str.length < length)
        str = '0' + str;
    return str;
}

alert(pad(2,5));
//--></script>

Feedback on 'Q183 How can I format a number, e.g. 2, as 02 and 00002?'

©2018 Martin Webb