
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Left Pad an Integer in Java with Zeros
Let us see an example first to understand how an integer looks with left padding −
888 //left padding with spaces 0000000999 //left padding with 7 zeros
Let us see an example to left pad a number with zero −
Example
public class Demo { public static void main(String[] args) { int val = 9899; System.out.println(String.format("%05d",val)); } }
Output
09899
Let us see another example that pads a greater number of zeros −
Example
public class Demo { public static void main(String[] args) { int val = 9899; System.out.println(String.format("%010d", val)); } }
Output
0000009899
Advertisements