How can I pad an integer with zeros on the left?

后端 未结 16 2317
南旧
南旧 2020-11-21 06:31

How do you left pad an int with zeros when converting to a String in java?

I\'m basically looking to pad out integers up to 9999

16条回答
  •  Happy的楠姐
    2020-11-21 07:13

    Here is how you can format your string without using DecimalFormat.

    String.format("%02d", 9)

    09

    String.format("%03d", 19)

    019

    String.format("%04d", 119)

    0119

提交回复
热议问题