Snipt.net is closing on December 31st, 2016

For more information, please visit this blog post.

snipt

Ctrl+h for KB shortcuts

JavaScript

Pad numbers to fixed places

1
2
3
4
function zeroPad(num, places) {
    var zero = places - num.toString().length + 1;
    return Array(+(zero > 0 && zero)).join("0") + num;
}
https://snipt.net/embed/e7ba08b4a2a6ccb74e3f6633346c8e18/
https://snipt.net/raw/e7ba08b4a2a6ccb74e3f6633346c8e18/
e7ba08b4a2a6ccb74e3f6633346c8e18
js
JavaScript
4
2016-12-08T07:56:29
True
False
False
/api/public/snipt/107386/
pad-numbers-to-fixed-places
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="kd">function</span> <span class="nx">zeroPad</span><span class="p">(</span><span class="nx">num</span><span class="p">,</span> <span class="nx">places</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="kd">var</span> <span class="nx">zero</span> <span class="o">=</span> <span class="nx">places</span> <span class="o">-</span> <span class="nx">num</span><span class="p">.</span><span class="nx">toString</span><span class="p">().</span><span class="nx">length</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> </span><span id="L-3"><a name="L-3"></a> <span class="k">return</span> <span class="nb">Array</span><span class="p">(</span><span class="o">+</span><span class="p">(</span><span class="nx">zero</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="nx">zero</span><span class="p">)).</span><span class="nx">join</span><span class="p">(</span><span class="s2">&quot;0&quot;</span><span class="p">)</span> <span class="o">+</span> <span class="nx">num</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
js, number, pad, places
Copyrighted, illegal, or inappropriate content? Email [email protected].