Skip to content

Instantly share code, notes, and snippets.

@moqmar
Last active February 13, 2017 16:36
Show Gist options
  • Save moqmar/9ef4f0755d0a17b61458f35b91447bc1 to your computer and use it in GitHub Desktop.
Save moqmar/9ef4f0755d0a17b61458f35b91447bc1 to your computer and use it in GitHub Desktop.
Leftpad, as it has to be: simple, fast, flexible
// Leftpad - P(value, length, character, rightpad) - https://gist.github.com/moqmar/9ef4f0755d0a17b61458f35b91447bc1
function P(v,l,c,r) { v = v.toString(); while (v.length < l) v = r ? v + (c||0) : (c||0) + v; return v; }
// Usage: P(value, length, character, rightpad)
// P(25, 3) ➜ "025"
// P(1, 3, " ") ➜ " 1"
// P(25, 4, ".", true) ➜ "25.."
// public domain / cc0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment