anton.web.id

just do it √

Skip to content
  • Home
  • Code
  • Logs
  • Quote
  • About Me

Javascript: Add Leading Zero

Use this function to add leading zero in javascript:
function pad(n, width, z) {
  z = z || '0';
  n = n + '';
  return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
Example usage:
pad(10, 4);      // 0010
pad(9, 4);       // 0009
pad(123, 4);     // 0123
pad(10, 4, '-'); // --10
credit : http://stackoverflow.com/a/10073788
This entry was posted in Code on Sunday, June 29
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post

Archives

  • ►  2017 (1)
    • ►  May (1)
  • ►  2016 (1)
    • ►  November (1)
  • ►  2015 (2)
    • ►  May (2)
  • ▼  2014 (7)
    • ►  October (3)
    • ▼  June (1)
      • Javascript: Add Leading Zero
    • ►  March (2)
    • ►  January (1)
  • ►  2012 (4)
    • ►  August (2)
    • ►  March (2)

Reading List

Loading...
  • aantonw
  • @aantonw
  • aantonw