views:

625

answers:

8

I just wrote this incredibly verbose code to turn numbers like 2 into 02. Can you make this function shorter, please (maintaning the functionality)?

 public static function format(n:int, minimumLength:int):String {
  var retVal:String = n.toString();
  var stillNeed:int = minimumLength - retVal.length;
  for (var i:int = 0; i < stillNeed; i++) {
   retVal = "0" + retVal;
  }
  return retVal;
 }

Please use types for variables. Extra points (good-vibe points, not SO points) if there's already a built-in function that I don't know about.

If anybody wants to post some extremely short equivalent in some other language, that would be fun too.

+2  A: 

I don't think there is a built-in way, but this might be cleaner (if not necessarily better performing):

//20 zeroes, could be more if needed
public static var Zeroes:String = "00000000000000000000"   

public static function format(n:Number, minimumLength:int):String {
var retVal:String = (n.toFixed(0)); // cut off the decimals
var stillNeed:int = minimumLength - retVal.length;
retVal = Zeroes.substring(0, stillNeed) + retVal; 
return retVal;
}

The "zeroes" var eliminates the need for looping, just prepend however many zeroes you need from a prebuilt string.

Dave Swersky
That's actually quite nice. I like it. Thanks.
Yar
Please note that I changed the question slightly because my original code was a bit more annoying (changed the Number to int incorrectly).
Yar
This answer has several advantages: the static String is instantiated just once, and the substring function probably makes one string. How do you know how many zeroes to use?
Yar
Use more than you think you'll need... if the longest "minimum length" is 50 then use 100 just to be safe, etc. If that method is used an exception should be thrown if the minimum length is over the length of the static zeroes string.
Dave Swersky
A if-throws would take us out of the conciseness thing...
Yar
+4  A: 

Christophe Herreman almost got it right, but his method adds more zeroes and not the differential amount. I fixed it a bit:

public static function format(n:int, minimumLength:int):String {
  var v:String = n.toString();
  var stillNeed:int = minimumLength - v.length;       
  return (stillNeed > 0) ? v : String(Math.pow(10, stillNeed) + v).substr(1);
}

My earlier try:

 public static function format(n:int, minimumLength:int):String {
    var stillNeed:int = minimumLength - n.toString().length;               
    return (n.split("").reverse().join("") as int) // 32 -> 23
             *Math.pow(10, stillNeed > 0 ? stillNeed : 0).toString() // 23000
                 .split("").reverse().join("");  // 00032
 }

 public static function formatAny(n:Number, minimumLength:int):String {
    return format((int)n) + n.toString().split('.')[ 1 ];
 }

 // use this if you want to handle -ve numbers as well
 public static function formatAny(n:Number, minimumLength:int):String {
    return (n < 0 ? '-' : '') + formatAny(n, minimumLength);
 }
dirkgently
Wow, brilliant... I'll have to try this one out. Thank you!
Yar
Hold on a minute: Wouldn't this take the number 32.3 with 5 zeroes minium and turn it into 00023? The reverse would also reverse any number over 9...
Dave Swersky
I have my fingers crossed: I don't have my FB3 which died awhile ago to test this. So, guys ... ;)
dirkgently
Okay! Got something for Numbers as well. I still don't take into account negatives ;)
dirkgently
You're using reverse like it's on String, but it's on Array. So my last line for your format function looks like this return s.split("").reverse().join("");
Yar
@Daniel: Don't get it; Can you post it here?
dirkgently
See my answer below... thanks!
Yar
@Dave, right on, they do come out sort of backwards :) Beginning to think my original method wasn't that bad...
Yar
Interesting and a geeat answer. I can't use it because it's too tricky for me to consider it good code. But it's good code :)
Yar
+3  A: 

How about this:

public static function format(n:int, len:int):String {
  var v:String = n.toString();
  return (v.length >= len) ? v : String(Math.pow(10, len) + n).substr(1);
}

There is not built-in function to do this btw. If you need decent padding functions, take a look at the StringUtils in Apache Commons Lang.

Christophe Herreman
+1  A: 

"If anybody wants to post some extremely short equivalent in some other language, that would be fun too."

In javascript it is easy - paste this into your browser's address bar

javascript: function zit(n, w) {var z="000000000000000000"; return (z+n).substr(-w);} alert(zit(567, 9)); void(0);

Dipstick
Beauty. I still have room in my heart for languages I've always hated...:)
Yar
Although on second thought, it's equivalente to Dave's answer above... though that -w possible only Javascript is sweet. Thanks again.
Yar
+4  A: 

This wouldn't be the fastest implementation (it does some unnecessary copying and has a loop), but it is nice and readable:

public static function pad(num:int, minLength:uint):String {
    var str:String = num.toString();
    while (str.length < minLength) str = "0" + str;
    return str;
}
grapefrukt
Nice one. Internally, it's the same as mine, but code-wise, it's far more readble.
Yar
+1  A: 

I've always done this by taking a string that is the maximum padded width of zeros containing all zeros, then appeneding the string to padded to the end of the zeros string and then using substring to get the right hand Length digits.

Something like:

function pad(num:int, length:unit):String{
    var big_padded:String "0000000000000000000000000000" + num.toString();
    return big_padded.substring(big_padded.length - length);
 }
Richard Harrison
1) Like Dave's answer, but a bit more sexy. 2) the zeroes string should be a static so we take the hit just once 3) again, HOW do we know how many zeroes to put in the String? THANKS!
Yar
put in as many zeros as you may possibly ever need. You can check for errors by comparing the zero string length to that of the length parameter for safety.
Richard Harrison
Thanks Richard. That is in fact the question. How many can you possibly need. Possible answers should have something to do with max length of Number, IMO.
Yar
+1  A: 

The as3corelib package put out by adobe has a nice little NumberFormatter class that uses a series of STATIC classes. In this case you could use the addLeadingZero function.

//The following method is from the NumberFormatter class of the as3corelib package by Adobe.
public static function addLeadingZero(n:Number):String
{
var out:String = String(n);

if(n < 10 && n > -1)
{
    out = "0" + out;
}

return out;
}

I included the function just to show it's simplicity, but I would use the package instead of yoinking the function because it provides many other useful features like StringUtils, encryption methods like MD5, blowfish, etc.

You can download the package here For newer users you must provide a classpath to where this package lives. It is also smart to import the classes instead of using their fully qualified class names.

Brian Hodge
+1  A: 

Props to dirkgently and all others who have responded here, but apparently people are voting up without actually trying the code.

dirkgently's final function is mostly correct, but his '>' needs to be a '<'.

This function performs as desired (tested fairly thoroughly):

public static function format(n:int, minimumLength:int):String {
  var v:String = n.toString();
  var stillNeed:int = minimumLength - v.length;
  return (stillNeed < 0) ? v : String(Math.pow(10, stillNeed) + v).substr(1);
}
Not that it's relevant, but I do test all code (when it's my question). I do not test and correct code, which would be another level of work :) Thanks!
Yar
At the same time, I don't see what's better about the dirkgently function over my original... it's neither more readable, nor does it seem to reduce processor cycles (guessing, I don't mean empirically)... I like the one I chose as best answer since it's actually kind of nice, readable, and works.
Yar