Java - How to format a number to a 2 char string?

后端 未结 3 1899
日久生厌
日久生厌 2021-02-18 12:56

How would I format an int between 0 and 99 to a string which is 2 chars. E.g

4  becomes \"04\"
36 becomes \"36\"

Than

3条回答
  •  悲&欢浪女
    2021-02-18 13:18

    Simple way is to append "" to the integer (or any primitive type)

    String str = "" + anyInetger;
    

    You can use

    String str;
    if (anyInteger < 10)
    {
        str = " 0" + anyInetger;
    }
    else
    {
        str = "" + anyInetger;
    }
    

提交回复
热议问题