// n = number you want padded // digits = length you want the final output function blankPad(n, digits) { n = n.toString(); while (n.length < digits) { n = ' ' + n; } return n; }I think this function is rather elegant since the `while` condition states what the function is required to do. It handles an impossible request to pad number 12345 to 3 digits. Since this adds the space characters to the string one-at-time it could be call slow but its typically only used for a small number of spaces.
Programming Tips - javaScript: javaScript has no sprintf() so how to blank pad a number?
Date: 2008may13
Update: 2025oct21
Language: javaScript
Q. javaScript: javaScript has no sprintf() so how to blank pad a number?
A. Use this function: