8
\$\begingroup\$

This is a switch statement in JavaScript. I have a feeling that it can be done in a shorter way.

switch(n.length){ case 1: n = '00000' + n; break; case 2: n = '0000' + n; break; case 3: n = '000' + n; break; case 4: n = '00' + n; break; case 5: n = '0' + n; break; } 
\$\endgroup\$
2
  • \$\begingroup\$ I'm curious what this is being used for. It's pretty non-general. \$\endgroup\$ Commented Nov 6, 2011 at 20:00
  • \$\begingroup\$ @HackSaw I believe it's used for left-padding a number. There's a better padding function out there as well: click me \$\endgroup\$ Commented Nov 6, 2011 at 23:57

4 Answers 4

22
\$\begingroup\$
function foobar(n) { var zeroes = "000000"; return zeroes.substr(n.length) + n.toString(); } 
\$\endgroup\$
4
  • \$\begingroup\$ ps: there's a small error in the code, it should be: zeroes.substr(n.length) + n.toString(); \$\endgroup\$ Commented Nov 6, 2011 at 12:42
  • \$\begingroup\$ Then why you don't use n.length in your code? \$\endgroup\$ Commented Nov 6, 2011 at 13:07
  • \$\begingroup\$ lol sorry, the same mistake is in my code :) anyway thank you, I just needed the idea \$\endgroup\$ Commented Nov 6, 2011 at 13:12
  • 12
    \$\begingroup\$ You don’t need the variable. The name zeroes doesn’t add any meaning, so "000000".substr(n.length) is just as clear, and more succinct. \$\endgroup\$ Commented Nov 6, 2011 at 22:53
7
\$\begingroup\$

Well, the value of n is known in each case, so you can just put it in the string:

switch(n) { case 1: n = '000001'; break; case 2: n = '00002'; break; case 3: n = '0003'; break; case 4: n = '004'; break; case 5: n = '05'; break; } 

If n can only have one of these values, you can use it as index in an array:

n = ['000001','00002','0003','004','05'][n - 1]; 

Edit:

With the edited code (using n.length in the switch), it would be:

n = ['00000','0000','000','00','0'][n.length - 1] + n; 
\$\endgroup\$
0
6
\$\begingroup\$

It seems you're trying to implement part of the functionality of printf. Use one of the existing implementations, e.g. dive.into.javascripts sprintf to do the zero-padding:

n = sprintf("%06d", n); 
\$\endgroup\$
2
\$\begingroup\$

Currently the other answers do not provide a robust way of doing this. They either use magic numbers or create long strings of zeroes manually. So I am adding this answer as a reference for anyone visiting this question in the future.

We can create the padding by joining an empty array together. I have provided two options: straight code or a function for reuse.

Straight Code:

var desiredLength = 6; if (n.length < desiredLength) { n = Array(desiredLength - n.length + 1).join("0") + n; } 

Function:

function padTo(length, str, ch) { if (str.length < length) { return Array(length - str.length + 1).join(ch) + str; } return str; } // Usage padTo(6, "3", "0")); // 000003 
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.