Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to left pad Integer with zeroes in java

In this post, we will see how to left pad Integer with zeroes in java.
There are multiple ways to left pad Integer with zeroes in java.

Using String’s format() method

You can use String‘s format() method to add leading zeroes to Integer in java. This is simplest and elegant solution to left pad Integer with zeroes in java.
Let’s say you want to add 3 leading zeroes to Integer of lenght 2, so you need to provide following format:

Here is an example:

package org.arpit.java2blog;

public class PadzeroestringFormat {

    public static void main(String[] args) {
        int i = 12;
        System.out.printf("%05d",i);
    }
}

Output:

00012

Using DecimalFormat

You can also use DecimalFormat‘s format() method to left pad Integer with zeroes. You need to pass format with all 0 of total length to do this.
For example:

int i = 12;

DecimalFormat df = new DecimalFormat("00000");
String padLeftStr = df.format(i);
System.out.println(padLeftStr);  // Prints 00012

DecimalFormat df = new DecimalFormat("000");
String padLeftStr = df.format(i);
System.out.println(padLeftStr);  // Print 0012

Here is an example:

package org.arpit.java2blog.entry;

import java.text.DecimalFormat;

public class PadZeroDecimalFormat {

    public static void main(String[] args) {
        int i = 12;
        DecimalFormat df = new DecimalFormat("00000");
        String padLeftStr = df.format(i);
        System.out.println("Formatted Integer with 3 leading zeroes: "+padLeftStr);
    }
}

Output:

Formatted Integer with 3 leading zeroes: 00012

Using NumberFormat

You can use NumberFormat’s format() method with setMaximumIntegerDigits() and setMinimumIntegerDigits() to add leading zeroes to Integer in java.
Here is an example:

package org.arpit.java2blog.entry;

import java.text.NumberFormat;

public class PadZeroNumberFormat {

    public static void main(String[] args) {

        int i=12;
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumIntegerDigits(5);
        numberFormat.setMinimumIntegerDigits(5);
        numberFormat.setGroupingUsed(false);

        String padLeftStr = numberFormat.format(i);
        System.out.println("Formatted Integer with 3 leading zeroes: "+padLeftStr);
    }
}

Output:

Formatted Integer with 3 leading zeroes: 00012

Using apache common lang3

You can use Apache common’s StringUtils class to pad integer with leading zeroes.
Here is the dependency which you need to add for Apache common lang3 in pom.xml.

org.apache.commonscommons-lang33.9

Here, we are using StringUtils.leftPad() method. Here are the overloaded version of leftPad.

/**
 * @param str       - the String to pad out, may be null
 * @param size      - Final length of the string
 * @param padChar   - character or string which you want to use for left pad
 * 
 * @return left pad string or original string if padding is not required.
 */

public static String leftPad(final String str, final int size) {...}
public static String leftPad(final String str, final int size, String padStr) {...}
public static String leftPad(final String str, final int size, final char padChar) { ... }

Here is an example:

package org.arpit.java2blog.entry;

import org.apache.commons.lang3.StringUtils;

public class PadzeroestringUtils {

    public static void main(String[] args) {
        int i = 12;
        String padLeftStr = StringUtils.leftPad(""+i,5,'0');
        System.out.println("Formatted Integer with 3 leading zeroes: "+padLeftStr);
    }
}

Output:

Formatted Integer with 3 leading zeroes: 00012

Using Guava library

You can also use Guava’s Strings class to left pad String.
Here is the dependency which you need to add for guava in pom.xml.

com.google.guavaguava29.0-jre

Here, we are using Strings.padStart() method. The overloaded version of padStart() are:

/**
 * @param string       - the String to pad out, it may be null
 * @param minLength      - Final length of the string
 * @param padChar   - character or string which you want to use for left pad
 * 
 * @return left pad string or original string if padding is not required.
 */

public static String padEnd(String string, int minLength, char padChar){...}

Here is an example:

package org.arpit.java2blog;

import com.google.common.base.Strings;

public class PadZeroGauva {

    public static void main(String[] args) {
        int i = 12;
        String padLeftStr = Strings.padStart("" + i, 5, '0');
        System.out.println("Formatted Integer with 3 leading zeroes: "+padLeftStr);
    }

}

Output:

Formatted Integer with 3 leading zeroes: 00012

Using System.out.printf

If you just want to print left pad Integer with zeroes in java, you can use System.out.printf().
Here is an example:

package org.arpit.java2blog.entry;

public class PadLeftPrintf {
    public static void main(String[] args) {

    }
}

Output:

Formatted Integer with 3 leading zeroes: 00012

Using StringBuilder

You can write your own solution using for loop and StringBuffer rather than using any library method.
Here is an example:

package org.arpit.java2blog.entry;

public class PadzeroestringBuilder {

    public static void main(String[] args) {

        int i=12;
        String str = ""+i;
        StringBuilder sb = new StringBuilder();
        int size=5;

        for (int leftzeroes=size-str.length(); leftzeroes>0; leftzeroes--) {
            sb.append('0');
        }
        sb.append(str);
        String padLeftStr = sb.toString();
        System.out.println("Formatted Integer with 3 leading zeroes: "+padLeftStr);
    }
}

Output:

Formatted Integer with 3 leading zeroes: 00012

Using String’s substring() method

You can use for loop and substring() method to left pad integer with zeroes in java.

First create a String of total length of all 0s and then use substring() to extract required left pad zeroes and concatenate original String to it.
Here is an example:

package org.arpit.java2blog.entry;

public class PadLeftSubstring {

    public static void main(String[] args) {

        int inputNo=12;
        String str = ""+inputNo;
        String padZero="";
        int size=5;
        for (int i = 0; i 

Output:

Formatted Integer with 3 leading zeroes: 00012

That’s all about how to left pad integer with zeroes in java.



This post first appeared on How To Learn Java Programming, please read the originial post: here

Share the post

How to left pad Integer with zeroes in java

×

Subscribe to How To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×