In this tutorial, we will be exploring three methods to convert data from primitive data type double to int in Java with examples:
We will be learning the following ways that are used for this conversion:
- Typecasting
- Math.round()
- Double.intValue()
=> Explore The Complete Java Training Series Here
Table of Contents:
Methods To Convert double To int In Java
double and int are primitive data types in Java. Primitive data type int is used to represent integer values like 1,100 etc. whereas double represents floating-point numbers like 1.5, 100.005, etc.
In Java programs, under some scenarios, input data to the program is available in Java double, but it is required to round it off i.e. to convert a number to have it without any floating-point.
In such scenarios, this double value is required to be converted to an int data type. For Example, to print average weight, height, etc., or bill generated, it is more preferred to represent the value as integer instead of number with floating-point.
Let’s see the various ways of converting Java double to int one by one in detail.
#1) Typecasting
In this way of conversion, double is typecast to int by assigning double value to an int variable.
Here, Java primitive type double is bigger in size than data type int. Thus, this typecasting is called ‘down-casting’ as we are converting bigger data type values to the comparatively smaller data type.
Let’s understand this down-casting with the help of the following sample code:
package com.softwaretestinghelp; /** * This class demonstrates sample code to convert double to int Java program * using typecast * * @author * */ public class DoubleToIntDemo1 { public static void main(String[] args) { // Assign 99.95 to double variable billAmt double billAmt = 99.95; System.out.println("billAmt :"+ billAmt); // Typecast billAmt // to convert double billAmt value to int // and assign it to int variable bill int bill = (int) billAmt; System.out.println(" Your generated bill amount is : $"+bill+". Thank You! "); } }
Here is the program Output:
billAmt: 99.95
Your generated bill amount is: $99. Thank You!
Here, the “99.95” value is assigned to double variable billAmt.
double billAmt = 99.95;
This is converted to an integer by downcasting to an int data type as shown below.
int bill = (int) billAmt;
Hence, when we print this bill value on the console:
System.out.println(" Your generated bill amount is : $"+bill+". Thank You! ");
We get the following output on the console:
Your generated bill amount is : $99. Thank You!
As we can see, the floating-point double value “99.95” is now converted to int value “99”.
This is the simplest way of converting double to int. Let’s have a look at some more ways of doing so.
#2) Math.round(double d) Method
The round() method is a static method of the class Math.
Let’s have a look at the method signature below:
public static long round(double d)
This static method returns the nearest long value of the argument. If the argument value is NaN, then it returns 0. For the argument value negative infinity, less than or equal Long.MIN_VALUE, it returns Long.MIN_VALUE.
Similarly, for argument value positive infinity greater than or equal Long. MAX_VALUE., the method returns Long. MAX_VALUE.
d is a floating-point value that is required to be rounded to a long value.
Let’s try to understand how to use this Math.round(double d) method with the help of the following sample program. In this program, the bill amount is generated with floating-point i.e. in double data type value.
We are retrieving the integer value of the bill amount using the Math.round(double d) method as shown below:
package com.softwaretestinghelp; /** * This class demonstrates sample code to convert double to int Java program * using Math.round() method * * @author * */ public class DoubleToIntDemo2 { public static void main(String[] args) { // Assign 25.20 to double variable firstBillAmt double firstBillAmt = 25.20; System.out.println("firstBillAmt :"+firstBillAmt); // Pass firstBillAmt as a parameter to Math.round() // to convert double firstBillAmt value // to long value and assign it to long variable bill1 long bill1 = Math.round(firstBillAmt); System.out.println("bill1 :"+bill1); //typecast bill1 to int to convert to int value and assign to int variable firstBill int firstBill = (int)bill1; System.out.println("Your first bill amount is : $"+firstBill+"."); // Assign 25.50 to double variable secondBillAmt double secondBillAmt = 25.50; System.out.println("secondBillAmt :"+ secondBillAmt); // Pass secondBillAmt as a parameter to Math.round() // to convert double secondBillAmt value // to long value and assign it to long variable bill2 long bill2 = Math.round(secondBillAmt); System.out.println("bill2 :"+bill2); //typecast bill2 to int to convert to int value and assign to int variable secondBill int secondBill = (int)bill2; System.out.println("Your second bill amount is : $"+secondBill+"."); } }
Here is the program Output:
firstBillAmt :25.2
bill1 :25
Your first bill amount is : $25.
secondBillAmt :25.5
bill2 :26
Your second bill amount is : $26.
Here, we are assigning values to double variables:
double firstBillAmt = 25.20; double = 25.50;
These values are passed as an argument to Math.round(double d) method:
long bill1 = Math.round(firstBillAmt); long bill2 = Math.round(secondBillAmt);
This converts the values into a long data type.
Further, these values are converted to int. This is because Math.round() returns a long value and we need to retrieve the int data type value.
This is done as follows:
int firstBill = (int)bill1; int secondBill = (int)bill2;
So finally, when we print the bill amounts on the console, we see the following outputs:
Your first bill amount is : $25.
Here the original double value was 25.2 which gets rounded off to the nearest integer 25.
Your second bill amount is : $26.
Here, the original double value was 25.5 which gets rounded off to the nearest integer 26.
Notice the difference between the first bill and the second bill amount. This is because the second bill was 25.5 i.e. the number after the decimal point is 5 and for the first bill, it is 25.2 i.e. 2 after the decimal point.
#3) Double().intValue() Method
This is an instance method of Double class.
Let’s have a look at the method signature below:
public int intValue()
This method converts the value represented by Double-object to primitive data type int and returns the int value.
Let’s understand the use of the intValue() method of Double class with the help of the sample program below. In this program, the average score calculated is a floating-point numeric value in double data type.
This is converted to data type int using the Double().intValue() method:
package com.softwaretestinghelp; /** * This class demonstrates sample code to convert double to int Java program * using new Double().intValue() method * * @author * */ public class DoubleToIntDemo2 { public static void main(String[] args) { // Assign 90.95 to double variable score1 double score1 = 90.95; System.out.println("score1 :"+score1); // Assign 80.75 to double variable score2 double score2 = 80.75; System.out.println("score2 :"+score2); // Assign 75.90 to double variable score3 double score3 = 75.90; System.out.println("score3 :"+score3); // Calculate average score double averageScoreNumber = (score1+score2+score3)/3; System.out.println(" Average Score Number is :"+averageScoreNumber); // Pass averageScoreNumber as a parameter to Double() // and invoke intValue() to convert double averageScoreNumber value // to int value and assign it to int variable average int average = new Double(averageScoreNumber).intValue(); //Print average score on the console System.out.println(" Congratulations ! You have scored :"+average); } }
Here is the program Output:
score1 :90.95
score2 :80.75
score3 :75.9
Average Score Number is :82.53333333333333
Congratulations ! You have scored :82
Here the floating-point score values are assigned to double variable as shown below:
double score1 = 90.95; double score2 = 80.75 double score3 = 75.90;
The average calculated for these 3 scores is also a floating-point number double value:
double averageScoreNumber = (score1+score2+score3)/3; System.out.println(" Average Score Number is :"+averageScoreNumber);
This prints the following on the console:
Average Score Number is :82.53333333333333
Now, this double value is converted to int using Double(double d) constructor which returns Double-object. Method intValue() is invoked on this Double-object to return the value of primitive data type int as shown below.
int average = newDouble(averageScoreNumber).intValue();
Hence, when we print the average on the console:
System.out.println(" Congratulations ! You have scored :"+average);
It prints the following on the console i.e. int value 82 for double value 82.53333333333333:
Congratulations ! You have scored :82
Note: From Java9, the constructor Double(double d) has been deprecated. Hence, this is less preferred since Java9.
With this, we have covered the various ways for converting a value from primitive data type double to int Java primitive data type.
Let’s have look at some of the frequently asked questions about the double to int conversion.
Frequently Asked Questions
Q #1) How do you convert a double to an int in Java?
Answer: In Java, the primitive data type double can be converted to primitive data type int using the following Java class methods and ways:
- typecasting: typecast to int
- Math.round()
- Double.intValue()
Q #2) What is int and double in Java?
Answer: In Java, there are various primitive data types like int, double, long, float to store a numeric value. Primitive data type int has size 4 bytes that holds whole numbers like 1 ,500 etc. starting from -2,147,483,648 to 2,147,483,647 .
Primitive data type double has size 8 bytes that hold floating-point numbers like 1.5, 500.5, etc. It can store 15 decimal digits. In Java, we can convert the value of the double data type to an int data type.
Q #3) How do you cast to int in Java?
Answer: In Java, the values in different data types can be converted to int like String to int or long to int by typecasting.
Also, there are various ways of casting double to int as shown below:
- typecasting
- Math.round()
- Double.intValue()
Q #4) Can you add an int and double in Java?
Ans: One of the ways if the desired result is expected to be in int data type, then, first it needs to convert data to int value and then perform the addition. This conversion can be done using typecasting, Double().intValue() and Math.round() methods.
Conclusion
In this tutorial, we learned how to convert primitive double data type value to data type int in Java using the following class methods in detail with examples.
- typecasting
- Math.round()
- Double.intValue()
=> Visit Here To See The Java Training Series For All