How to left pad an integer with zeros in Java

2 Answers

0 votes
public class MyClass {
    public static void main(String args[]) {
        int x = 17;
        
        System.out.printf("%05d", x);
    }
}





/*
run:
   
00017
   
*/

 



answered Sep 10, 2023 by avibootz
0 votes
import java.text.DecimalFormat;

public class MyClass {
    public static void main(String args[]) {
        int x = 17;
        
        DecimalFormat df = new DecimalFormat("00000");
        
        String str = df.format(x);
        
        System.out.println(str);
    }
}





/*
run:
   
00017
   
*/

 



answered Sep 10, 2023 by avibootz

Related questions

1 answer 124 views
124 views asked May 29, 2024 by avibootz
1 answer 129 views
129 views asked May 29, 2024 by avibootz
2 answers 176 views
2 answers 150 views
1 answer 200 views
1 answer 196 views
196 views asked Apr 28, 2016 by avibootz
...