JavaDevNotes.com

Java Integer To String With Leading Zeros

This post will show how to convert Java Int to String with leading zeroes. Example Java codes will be shown below.

Description

Given a number with several digits, we wish to convert it to a String instance of a specific length. If the length is greater than the number of digits, zeroes will be padded in front of the number.

For example, if we have the number

int number = 123;
And if we wish to convert the value to a String with length 5, since there are only 3 digits present in the number, our desired String value is:
String numberAsString = "00123";
And if we target the String to have a length of 10, the desired String value is:
String numberAsString = "0000000123";


Convert using String.format()

If you are using Java 5 (JDK1.5) or higher, the String.format() static method can be used to convert an int to String with leading zeroes. This is simple and straightforward.

Syntax

public static String format(String format, Object... args)

If we use this plainly with this code:

int number = 123;
String numberAsString = String.format ("%d", number);

The variable numberAsString will have the value of "123"

But we can specify special formatting to have leading zeroes:

Example #1
This code snippet:
int number = 123;
String numberAsString = String.format ("%05d", number);
System.out.println(numberAsString);
Will render this output:
00123

As you can see, when we changed "%d" to "%05d", it tells the method to pad the String with "0" to fill the String and have a length of 5.

Example #2
Similarly, here is another example code:

int number = 123;
String numberAsString = String.format ("%010d", number);
System.out.println(numberAsString);
It will have this output when executed:
0000000123

By using "%010d", it tells the method to pad the String with "0" to fill the String and have a length of 10.

This is a simple solution but only if you are using Java 5 or higher.



Convert using DecimalFormat

The class java.text.DecimalFormat is a class that formats a number to a String representation following a certain pattern. We can have this pattern put leading zeroes too.

Example #1
We can use the format "00000" (String of 5 zeroes) to force formatting the number to length of 5, and using leading zeroes if there are no enough digits in the number.

import java.text.DecimalFormat;
public class Testing {
   public static void main(String[] args) {
      int number = 123;
      DecimalFormat df = new DecimalFormat("00000");
      String numberAsString = df.format(number);
      System.out.println(numberAsString);
   }
}
This will output:
00123

Example #2
Just for clarity, here is another example to have a String with length 10.

import java.text.DecimalFormat;
public class Testing {
   public static void main(String[] args) {
      int number = 123;
      DecimalFormat df = new DecimalFormat("0000000000");
      String numberAsString = df.format(number);
      System.out.println(numberAsString);
   }
}
This will output:
0000000123

Example #3
Hard-coding the format is not a very elegant solution for converting and int to String with leading zeroes. Here is another approach:
int number = 123;
int digits = 20;
char[] zeros = new char[digits];
Arrays.fill(zeros, '0');
String format = String.valueOf(zeros);
DecimalFormat df = new DecimalFormat(format);
String numberAsString = df.format(number);
System.out.println(numberAsString);
And the output will be:
00000000000000000123


Custom Code

If you wish to challenge yourself and reinvent the wheel, there are many approach that can be done to write a custom code. Here is one example:
public class Testing {
   public static void main(String[] args) {
      int number = 123;
      String numberAsString = intToString(number, 10);
      System.out.println(numberAsString);
   }
   public static String intToString(int number, int stringLength) {
      int numberOfDigits = String.valueOf(number).length();
      int numberOfLeadingZeroes = stringLength - numberOfDigits;
      StringBuilder sb = new StringBuilder();
      if (numberOfLeadingZeroes > 0) {
         for (int i = 0; i < numberOfLeadingZeroes; i++) {
            sb.append("0");
         }
      }
      sb.append(number);
      return sb.toString();
   }
}

And this will output:

0000000123



Read my previous post if you wish to see example code on general ways of converting a Java Int to String.


String Tutorials And Examples