How to left pad with zeroes to a String in Java

In this article, we will discuss how to left pad with zeroes to a String. Actually, there are number of options and library available for this, but we will limit to 2 libraries

 

Left padding with Zeroes in a String:

  • Using String.format() from Java’s core library
  • Using StringUtils.leftPad() method from Apache commons

 

1. String.format() method – core Java library

Syntax:

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

 

Here, we will pad leading & trailing zeroes for below cases

  1. Left padding with 5 zeroes to an Integer value
  2. Left padding with 10 zeroes to an Integer value
  3. Left padding with 6 zeroes to a Float or Double value
  4. Left and right padding with zeroes to a Float or Double value (with integer part consist of 7 digits and floating part consists of 3 digits)
  5. adding positive sign (+ve) to an Integer value along with left padding zeroes
  6. adding negative sign (-ve) to an Float/Double value along with left padding zeroes to Integer part and right padding zeroes to Floating part
  7. manually adding negative sign

PaddingZeroesUsingFormat.java

package in.bench.resources.padding.zeroes.string;

public class PaddingZeroesUsingFormat {

	public static void main(String[] args) {

		// Ex-1: Left padding with 5 zeroes to an Integer value
		String str1 = "123";
		String str1Padded = String.format("%05d", 
				Integer.parseInt(str1)); 
		System.out.println("5 digits with left padded zeroes : "
				+ str1Padded);

		// Ex-2: Left padding with 10 zeroes to an Integer value
		String str2 = "542369";
		String str2Padded = String.format("%010d", 
				Integer.parseInt(str2)); 
		System.out.println("10 digits with left padded zeroes : " 
				+ str2Padded);

		// Ex-3: padding with zeroes to a Float value
		String str3 = "12.5";
		String str3Padded = String.format("%06.2f", 
				Float.parseFloat(str3)); 
		System.out.println("6 digits Float with padded zeroes : "
				+ str3Padded);

		// Ex-4: Left/right padding with zeroes to a Double value 
		String str4 = "64.24";
		String str4Padded = String.format("%010.3f", 
				Double.parseDouble((str4))); 
		System.out.println("10 digits Double with padded zeroes :" 
				+ str4Padded);

		// Ex-5: adding positive-sign (+ve) to an Integer-value  
		String str5 = "98612";
		String str5Padded = String.format("+%010d", 
				Integer.parseInt(str5)); 
		System.out.println("adding positive sign to 10 digits"
				+ " with padded zeroes : " 
				+ str5Padded); // along with left padding zeroes

		// Ex-6: adding negative sign (-ve) to an Float-value 
		String str6 = "-1545.56";
		String str6Padded = String.format("%011.3f", 
				Float.parseFloat(str6)); 
		System.out.println("adding negative sign to 10 digits"
				+ " with padded zeroes : "
				+ str6Padded); //  along with left padding zeroes

		// Ex-7: if negative sign (-ve) is not there, then add
		String str7 = "1545.56";
		String str7Padded = String.format("-%010.3f", 
				Float.parseFloat(str7)); 
		System.out.println("adding negative sign to 10 digits"
				+ " with padded zeroes : " 
				+ str7Padded);
	}
}

Output:

5 digits with left padded zeroes : 00123

10 digits with left padded zeroes : 0000542369

6 digits Float with padded zeroes : 012.50

10 digits Double with padded zeroes : 000064.240

adding positive sign to 10 digits with padded zeroes : +0000098612

adding negative sign to 10 digits with padded zeroes : -001545.560

adding negative sign to 10 digits with padded zeroes : -001545.560

 

2. StringUtils.leftpad() method – Apache Commons

Syntax:

public static String leftPad(String str, int size, char padChar);
public static String rightPad(String str, int size, char padChar);

 

Here, we will pad leading zeroes for below cases

  1. Left padding with 10 zeroes to an Integer value 12345
  2. Right padding floating part to a Float or Double value 12345.12

PaddingZeroes.java

package in.bench.resources.padding.zeroes.string;

import org.apache.commons.lang.StringUtils;

public class PaddingZeroes {

	public static void main(String[] args) {

		// Ex-1: Left padding with 10 zeroes to an Integer value
		String str1 = "986511";
		String str1Padded = StringUtils.leftPad(str1, 10, "0");
		System.out.println("10 digits with left padded zeroes : " 
				+ str1Padded);

		// Ex-2: LEFT padding with zeroes to a Float value
		String str2 = "12.5";
		String str2Padded = StringUtils.leftPad(str2, 6, "0");
		System.out.println("6 digits Float with padded zeroes : " 
				+ str2Padded);

		// Ex-3: RIGHT padding with zeroes to a Float value
		String str3 = "25.2";
		String str3Padded = StringUtils.rightPad(str3, 6, "0");
		System.out.println("6 digits Float with padded zeroes : " 
				+ str3Padded);
	}
}

Output:

10 digits with left padded zeroes : 0000986511

6 digits Float with padded zeroes : 0012.5

6 digits Float with padded zeroes : 25.200

 

References:

 

Happy Coding !!
Happy Learning !!

Interview program on String using null
Overriding toString() method to print values of ArrayList in Java