-1

Usually Integer.valueOf(), Integer.parseInt() works perfect. But i have problem if in a String object are some nulls(0), here is some code.

StringBuilder sb = new StringBuilder();

    sb.append("0");
    sb.append(9);

    Integer afterConvert = Integer.valueOf(sb.toString());
    System.out.println(afterConvert);

This code shows "9", but i need "09". Is any way to achieve it?

Artem Ruchkov
  • 581
  • 2
  • 7
  • 21

2 Answers2

2

This has nothing to do with null values.

The Integer value of 09 is 9.

If you want to print leasing zeros, use:

System.out.println(String.format("%02d", afterConvert));

See similar question here.

Community
  • 1
  • 1
BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
0

You cannot print Integer with leading 0s like this. You can use NumberFormat class:

NumberFormat nb = NumberFormat.getInstance();
nb.setMinimumFractionDigits(2);
System.out.println(nb.format(afterConvert));
dzielins42
  • 301
  • 1
  • 11