Grails Cookbook - A collection of tutorials and examples

Convert Java Integer To Fixed Length String

There are situations where you want to convert a Java Integer to Fixed Length String. This is typical when we want to align Strings for visual effects, or some formatting required by the next system that will consume the output. We will show how to convert numbers to fixed length String in this post.
Here is a simple example of what we want to achieve:
     Salary     Years        Age
       5000         2         40
       5675        11         35
       8075         5         25
As you could see, numbers are typically better for the eyes when aligned to the right. It helps us see which number is bigger, and easy for us to do quick calculations in our head as aligning to the right is how we are taught on how to add or subtract numbers.

Convert Using String.format()

Another option to utilize is the static method String.format(). It is also capable of getting you the converted integer to fixed length string. We will be using the 10 character-length we applied a while ago, and since we utilized zero to pad the String, let's do the same here. Observe and understand the written codes below:
public class SampleTesting {
	   public static void main(String[] args) {
		  // Padding zero to the string using String.format method.
	      int padNumber = 575;
	      String numberAsPaddedString = String.format("%010d", padNumber);
	      System.out.println("This is the result of the written pogram: " + numberAsPaddedString);
	   }
	}
Notice the parameter "%010d", the result shall have 0 prefix because there's an encoded zero before the "...10d".

This is the result of the written pogram: 0000000575

Let's repeat the same implementation only this time zero will be a space to pad the resulting string.
public class AnotherSampleTest {
   public static void main(String[] args) {
	  // Padding spaces to the string using String.format method.
      int padNumber = 153;
      String numberAsPaddedString = String.format("% 10d", padNumber);
      System.out.println("This is the result of the written pogram:" + numberAsPaddedString);
   }
}
This is the result of the written pogram:       153

Convert Using Substring Trick

Let's also try the same implementation with a substring trick to define the padded number of the String. Observe the example written below, it has 3 different numbers illustrating how the process works.

public class SubstringTricktesting { public static void main(String[] args) { // Padding a 1-digit number using a substring trick. int padNumber = 5; String convertNumberAsString = String.valueOf(padNumber); String numberAsPaddedString = "0000000000".substring(convertNumberAsString.length()) + convertNumberAsString; System.out.println("Padding a 1-digit number using a substring trick: " + numberAsPaddedString); // Padding a 2-digit number using a substring trick. padNumber = 10; convertNumberAsString = String.valueOf(padNumber); numberAsPaddedString = "0000000000".substring(convertNumberAsString.length()) + convertNumberAsString; System.out.println("Padding a 2-digit number using a substring trick: " + numberAsPaddedString); // Padding a 3-digit number using a substring trick. padNumber = 175; convertNumberAsString = String.valueOf(padNumber); numberAsPaddedString = "0000000000".substring(convertNumberAsString.length()) + convertNumberAsString; System.out.println("Padding a 3-digit number using a substring trick: " + numberAsPaddedString); } }

Check out the result below:
Padding a 1-digit number using a substring trick: 0000000005
Padding a 2-digit number using a substring trick: 0000000010
Padding a 3-digit number using a substring trick: 0000000175

Convert Using StringBuilder

One simple solution to acquire a fixed length is using StringBuilder. Let's use zero (0) to pad the prefix then add the number in the last portion. Check out the written program below:
public class SBTestSample {
   public static void main(String[] args) {
      int padNumber = 22;
      String numberAsPaddedString = String.valueOf(padNumber);
      StringBuilder sb = new StringBuilder();
      while(sb.length()+numberAsPaddedString.length()<15) {
         sb.append('0');
      }
      sb.append(padNumber);
      String newPaddedNumberAsString = sb.toString();
      System.out.println("Padded zero using StringBuilder: " + newPaddedNumberAsString);
   }
}
What the program did is it formatted the String to 15 characters long and appended zero before the value (22) in the variable number. Check out the result below:
Padded zero using StringBuilder: 000000000000022