public
Last active — forked from 140bytes/LICENSE.txt

Leading Zeros - 140byt.es

  • Download Gist
LICENSE.txt
1 2 3 4 5 6 7 8 9 10 11 12 13
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.
README.md
Markdown

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.

annotated.js
JavaScript
1 2 3 4 5 6 7 8
function(
a, // the number to convert
b // number of resulting characters
){
return (
[ 1e15 ] + a // combine with large number
).slice(-b) // cut leading "1"
}
index.js
JavaScript
1
function(a,b){return([1e15]+a).slice(-b)}
package.json
JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
{
"name": "pad",
 
"description": "Pad a given number with leading zeroes.",
 
"contributors": [
"aemkei",
"tsaniel",
"jed"
],
 
"keywords": [
"number",
"format"
]
}
test.html
HTML
1 2 3 4 5 6 7 8 9 10 11
<!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>

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

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

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

(42 bytes, and no eval)

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?

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.

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

Yep, that's the reason.

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

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

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

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.