How to pad integers with zeros and a width of N places using String.format() in Java

1 Answer

0 votes
package javaapplication1;

import java.io.IOException;

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {

        try {

            for (int i = 0; i < 10; i++) {
                String result = String.format("%1$06d %2$06d", i, i * 10);
                System.out.println(result);
            }

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
           
run:
     
000000 000000
000001 000010
000002 000020
000003 000030
000004 000040
000005 000050
000006 000060
000007 000070
000008 000080
000009 000090
  
 */

 



answered Nov 19, 2016 by avibootz

Related questions

3 answers 298 views
2 answers 164 views
1 answer 201 views
1 answer 171 views
...