In the previous article, we have seen Java Program to Get Ceiling Value of a number
In this article we will see how we can add zeros to the start of a number using Java programming language.
Java Program to Add Zeros to Start of a Number
In this article we will add zeros to the start of a number. For example if a number is 203 then after adding 2 zeros at start it will become 00203. Another example like if a number is 8965 after adding 4 zeros at start of it then the number will become 00008965.
Let’s see different ways to add zeros to the start of number.
Method-1: Java Program to Add Zeros to Start of a Number By Using String.format() Method
In Java String class we have inbuilt format() method which can be used to format an integer number to string. We can add any number of zeros at the start of the number.
Syantx: String.format("%0nd", Original_Number);
In, %0nd
‘n’ represents total number of digits.
Suppose we have number 567 and we want to add 4 zeros so that will be like %07d
as there were 3 digits already in number and 4 zeros will be added so total 7.
See below program to understand it clearly.
Approach:
- Declare an String variable say ‘
num
‘ and take the value as user input. - Declare an integer variable say ‘
numberOfZero
‘ and take the value as user input. - Declare an integer variable say ‘
result
‘ to hold the result. - Using the
String.format()
method add zeros at start of number. - Then print the result.
Program:
import java.util.Scanner; class Main { public static void main(String args[]) { //Scanner class object created Scanner sc = new Scanner(System.in); //String variable 'num' declared to hold the number String num; //integer variable 'NumberOfZero' to hold number of zeros value //which needs to be added before the original number int numberOfZero; //Taking input of number System.out.println("Enter a number : "); num = sc.next(); //Enter number of zeros to be added System.out.println("Enter total numbers of zeros to be added : "); numberOfZero = sc.nextInt(); //adding zeros by using the String.format String result = String.format("%0" +(num.length() + numberOfZero) + "d",Integer.valueOf(num)); System.out.println("After adding zeros the number is: " + result); } }
Output: Enter a number : 567 Enter total numbers of zeros to be added : 4 After adding zeros the number is : 0000567
Method-2: Java Program to Add Zeros to Start of a Number By Using DecimalFormat
First we have to create an object of DecimalFormat
based on specific format then we can call the format()
method.
DecimalFormat df = new DecimalFormat(zeros);
Where,
- zeros represents total number of digits after adding zeros at start.(write their digit zeros)
For example:
We have a number 897 as 3 digits and we need to add 2 zeros so the format will be like
DecimalFormat obj = new DecimalFormat(00000); //5-zeros total
We have another number 90 as 2 digits and we need to add 4 zeros so the format will be like
DecimalFormat obj = new DecimalFormat(000000); //6-zeros total
Approach:
- Declare an integer variable say ‘
num
‘ and assign the value to it. - Create object of
DecimalFormat
based on appropriate format. - Then using
format()
method add zeros. - Print result.
Program:
import java.text.DecimalFormat; import java.util.Scanner; class Main { public static void main(String args[]) { //integer variable 'num' initialized with actaul number int num = 567; //to add 4 zeros before actaul number //here we have written DecimalFormat("0000000") total 7 zeros //as 4 zeros will be added in start and //next 3 zeros will be represented with our actual number DecimalFormat df = new DecimalFormat("0000000"); //printing result System.out.println("After adding zeros the number is: " + df.format(num)); } }
Output: After adding zeros the number is: 0000567
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Related Java Programs: