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 118 views
118 views asked May 29, 2024 by avibootz
1 answer 125 views
125 views asked May 29, 2024 by avibootz
2 answers 171 views
2 answers 148 views
1 answer 197 views
1 answer 195 views
195 views asked Apr 28, 2016 by avibootz
...