Format an Integer using Java String Format

前端 未结 3 1934
小鲜肉
小鲜肉 2020-12-07 17:01

I am wondering if it is possible, using the String.format method in Java, to give an integer preceding zeros?

For example:

1 would become 001
2 would be

3条回答
  •  Happy的楠姐
    2020-12-07 17:53

    Use %03d in the format specifier for the integer. The 0 means that the number will be zero-filled if it is less than three (in this case) digits.

    See the Formatter docs for other modifiers.

提交回复
热议问题