Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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>

tsaniel commented Aug 30, 2011

Change substr to slice and save 1 byte.
Also change Math.pow(10,count) to eval('1e'+count), and the placing of coercion.

function(a,b){return(eval('1e'+b)+a+'').slice(1)}

Owner

aemkei commented Aug 30, 2011

Nice: This saved 5 chars, so we are now at 49 bytes!

jed commented Aug 30, 2011

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

(42 bytes, and no eval)

Owner

aemkei commented Aug 31, 2011

JEDは、先生です!If we limit the count to 9 it would save an extra byte a+1e9+"" - but 15 places are more flexible.

BTW: Why does it not work with 1e16?

1e16 + 3 === 10000000000000004

Can you point me to some resources with all the JS math limit stuff?

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 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

qfox commented Aug 31, 2011

Yep, that's the reason.

tsaniel commented Oct 8, 2011

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

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

jed commented Oct 8, 2011

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

Owner

aemkei commented Oct 8, 2011

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

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