¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

Re: How to add leading zeros to a number


 

¿ªÔÆÌåÓý

// From StackExchange - Function to pad with leading zeros
function pad(n, width, z) {
? ? z = z || '0';
? ? n = n + '';
? ? return n.length >= width ? n : new Array(width - n.length + 1 ).join(z) + n;
}

Examples:
pad(10, 4);      // 0010
pad(9, 4);       // 0009
pad(123, 4);     // 0123

pad(10, 4, '-'); // --10

Regards Dwayne

Join [email protected] to automatically receive all group messages.