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

后端 未结 16 2533
南旧
南旧 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条回答
  •  遥遥无期
    2020-11-21 06:54

    Found this example... Will test...

    import java.text.DecimalFormat;
    class TestingAndQualityAssuranceDepartment
    {
        public static void main(String [] args)
        {
            int x=1;
            DecimalFormat df = new DecimalFormat("00");
            System.out.println(df.format(x));
        }
    }
    

    Tested this and:

    String.format("%05d",number);
    

    Both work, for my purposes I think String.Format is better and more succinct.

提交回复
热议问题