Skip to content

Instantly share code, notes, and snippets.

@aemkei
Forked from 140bytes/LICENSE.txt
Created August 30, 2011 08:50
  • Star 13 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save aemkei/1180489 to your computer and use it in GitHub Desktop.
Leading Zeros - 140byt.es

Leading Zeros - 140byt.es

Pad a given number with leading zeroes. Useful to make it a fixed width like with e.g. sprintf in other programming langages.

  • Example: 00001, 00012, 00123, 01234, 12345

Usage

pad(number, count);
// returns number with leading zeros

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter. And check my "JavaScript Golf Lessons" slides.

function(
a, // the number to convert
b // number of resulting characters
){
return (
[ 1e15 ] + a // combine with large number
).slice(-b) // cut leading "1"
}
function(a,b){return([1e15]+a).slice(-b)}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "pad",
"description": "Pad a given number with leading zeroes.",
"contributors": [
"aemkei",
"tsaniel",
"jed"
],
"keywords": [
"number",
"format"
]
}
<!DOCTYPE html>
<title>Leading Zeros</title>
<pre id="output"></pre>
<script>
for (var i=10; i--;){
function pad(a,b){return([1e15]+a).slice(-b)}
document.getElementById("output").innerHTML += pad(
Math.pow(3, i+1), 5
) + "\n";
}
</script>
@jed
Copy link

jed commented Aug 31, 2011

to be honest, it's not my forte. i defer to the REAL sensei, like peter van der zee and mathias bynens. i'm more of a pragmatist, and just worked my way down until it did what i wanted it to.

@tsaniel
Copy link

tsaniel commented Aug 31, 2011

I don't know much, but i believe it's because JavaScript uses the "double-precision 64-bit binary format IEEE 754 value" to repersent numbers, and it always causes such a problem.
Another typical problem:
0.1 + 0.2 = 0.30000000000000004
http://es5.github.com/#x8.5

@pvdz
Copy link

pvdz commented Aug 31, 2011

Yep, that's the reason.

@tsaniel
Copy link

tsaniel commented Oct 8, 2011

Inspired by @LeverOne's trick, we can save another byte.

function(a,b){return([1e15]+a).slice(-b)}

@jed
Copy link

jed commented Oct 8, 2011

nice one, @tsaniel. funny how the logic is also completely different: you took out all the math!

@aemkei
Copy link
Author

aemkei commented Oct 8, 2011

Now down to 41 bytes, guys. - And I love the evolution of the original code!

@lcb
Copy link

lcb commented Jan 6, 2020

It might be good to highlight the limitations of this approach.
if a > 20 characters you run into scientific notation problems ([ 1e15 ] + 1234567891012345678901) = "10000000000000001.2345678910123457e+21"
if b is shorter than a your number will be truncated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment