How can we add leading zeros to the number in Java

0 votes
I want to add leading zeros to a number, on which I am going to perform further operations, how can I do that?
May 25, 2018 in Java by Daisy
• 8,140 points
5,086 views

2 answers to this question.

0 votes

To add zeros to a number, we can use the code given below.

String modified = String.format("%03d", num);
System.our.println(modified);
answered May 25, 2018 by Akrati
• 3,190 points
Take more care - it's System.out.println, not System.our.println
0 votes

From Java 1.5 you can use the String.format method. For example, 

String format = String.format("%%0%dd", digits);
String result = String.format(format, num);
return result;
answered Aug 26, 2019 by Sirajul
• 59,230 points

Related Questions In Java

0 votes
2 answers

How can we add the local JAR files to the Maven Project in Java?

Firstly I would like to give credit ...READ MORE

answered Nov 5, 2018 in Java by Sushmita
• 6,920 points
11,589 views
0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,920 points
12,940 views
0 votes
1 answer

How can we upload the files using JSP/ Servlets in Java?

<form action="upload" method="post" enctype="multipart/form-data"> ...READ MORE

answered Jun 8, 2018 in Java by Sushmita
• 6,920 points
844 views
0 votes
2 answers

How can we convert java.util.Date to java.sql.date in Java?

It's easy to convert a java.util.Date object ...READ MORE

answered Aug 27, 2019 in Java by Sirajul
• 59,230 points
1,289 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,253 views
0 votes
1 answer

How to pad an integer with zeros on the left in Java?

Use java.lang.String.format() method. String.format("%05d", number ...READ MORE

answered May 31, 2018 in Java by Daisy
• 8,140 points
2,223 views
0 votes
1 answer

What are optional parameters in Java

Using three dots: public void move(Object... x) { ...READ MORE

answered Apr 27, 2018 in Java by developer_1
• 3,350 points
851 views
0 votes
1 answer

How to reverse an int array in Java?

for(int i = 0; i < Data.length ...READ MORE

answered May 11, 2018 in Java by Akrati
• 3,190 points
956 views
+1 vote
6 answers

How can I separate the digits of an int number in Java?

You can also have a look here: To ...READ MORE

answered Dec 9, 2020 in Java by Roshni
• 10,520 points
208,818 views
0 votes
2 answers

How to break the nested loop in Java?

You can use break with a label for the ...READ MORE

answered Sep 20, 2018 in Java by Sushmita
• 6,920 points
1,188 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP