16

I have an integer 100, how do I format it to look like 00000100 (always 8 digits long)?

Jonik
  • 80,077
  • 70
  • 264
  • 372
Stig Christian
  • 287
  • 1
  • 5
  • 14

8 Answers8

35

Try this:

String formattedNumber = String.format("%08d", number);
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • Each time it is called a new formater instance is created. So I have to admit that with large amounts of data it can cause big memory problems. – Tomasz Gutkowski Sep 25 '17 at 13:23
11

You can also use the class DecimalFormat, like so:

NumberFormat formatter = new DecimalFormat("00000000");
System.out.println(formatter.format(100)); // 00000100
João Silva
  • 89,303
  • 29
  • 152
  • 158
3

Yet another way. ;)

int x = ...
String text = (""+(500000000 + x)).substring(1);

-1 => 99999999 (nines complement)

import java.util.concurrent.Callable;
/* Prints.
String.format("%08d"): Time per call 3822
(""+(500000000+x)).substring(1): Time per call 593
Space holder: Time per call 730
 */
public class StringTimer {
    public static void time(String description, Callable<String> test) {
        try {
            // warmup
            for(int i=0;i<10*1000;i++)
                test.call();
            long start = System.nanoTime();
            for(int i=0;i<100*1000;i++)
                test.call();
            long time = System.nanoTime() - start;
            System.out.printf("%s: Time per call %d%n", description, time/100/1000);
        } catch (Exception e) {
            System.out.println(description+" failed");
            e.printStackTrace();
        }
    }

    public static void main(String... args) {
        time("String.format(\"%08d\")", new Callable<String>() {
            int i =0;
            public String call() throws Exception {
                return String.format("%08d", i++);
            }
        });
        time("(\"\"+(500000000+x)).substring(1)", new Callable<String>() {
            int i =0;
            public String call() throws Exception {
                return (""+(500000000+(i++))).substring(1);
            }
        });
        time("Space holder", new Callable<String>() {
            int i =0;
            public String call() throws Exception {
                String spaceHolder = "00000000";
                String intString = String.valueOf(i++);
                return spaceHolder.substring(intString.length()).concat(intString);
            }
        });
    }
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

If Google Guava is an Option:

String output = Strings.padStart("" + 100, 8, '0');

Alternatively Apache Commons Lang:

String output = StringUtils.leftPad("" + 100, 8, "0");
Dag
  • 10,079
  • 8
  • 51
  • 74
2

String.format uses a format string which is described here

stacker
  • 68,052
  • 28
  • 140
  • 210
2

If you just need to print it out, this is a shorter version:

System.out.printf("%08d\n", number);
Denis Tulskiy
  • 19,012
  • 6
  • 50
  • 68
0

This also works:

int i = 53;
String spaceHolder = "00000000";
String intString = String.valueOf(i);
String string = spaceHolder.substring(intString.lenght()).contract(intString);

But the other examples are much easier.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
0

If you need to parse this string and or support i18n consider extending the

java.text.Format 

object. Use the other answers to help you get the format.

Adam Gent
  • 47,843
  • 23
  • 153
  • 203