Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Padding to get specific number of digits in a number

String val = "98"

I need to get the output as 0000098 (7 digits).

I need left padding of zeros to the string or integer value...

The number stored in val is dynamic and may contain any number of digits, but the output should always be 7 digits.

like image 700
Bharath A N Avatar asked Jul 13 '12 07:07

Bharath A N


1 Answers

Use String.format:

public class A {
  public static void main(String[] args) {
    System.out.println(String.format("%07d", 98)); // -> 0000098
  }
}
like image 93
daniel kullmann Avatar answered Oct 20 '22 16:10

daniel kullmann