Format an Integer using Java String Format

前端 未结 3 1930
小鲜肉
小鲜肉 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条回答
  •  独厮守ぢ
    2020-12-07 17:41

    String.format("%03d", 1)  // => "001"
    //              │││   └── print the number one
    //              ││└────── ... as a decimal integer
    //              │└─────── ... minimum of 3 characters wide
    //              └──────── ... pad with zeroes instead of spaces
    

    See java.util.Formatter for more information.

提交回复
热议问题