- Java – Convert double to int example
- 1. Java – double to int conversion using type casting
- 2. Java – Convert double to int using Math.round()
- Convert double to int in Java using Double.intValue()
- Top Related Articles:
- About the Author
- How to convert double to int in Java? Example
- double to int in Java — Typecasting
- double to int — Math.round()
- Double to int in Java — Double.intValue()
- Important points
- How to Convert double to int in Java
- How to Convert double to int in Java?
- Method 1: Convert double to int in Java by Using TypeCasting
- Method 2: Convert double to int in Java by Using Double.intValue() Method
- Method 3: Convert double to int in Java by Using Math.round() Method
- Conclusion
- About the author
- Farah Batool
Java – Convert double to int example
In this tutorial, we will learn how to convert double to int in Java. As we know double value can contain decimal digits (digits after decimal point), so when we convert double value with decimal digits to int value, the decimal digits are truncated. In this java tutorial, we will see three ways to convert a double value to int. Out of these 3 ways there is one way in which you can round of the double value to nearest integer value.
1. Convert double to int using typecasting
2. Convert double to int using Math.round() – This ensures that the decimal digits double value is rounded of to the nearest int value.
3. Convert double to int using Double.intValue()
1. Java – double to int conversion using type casting
To typecast a double value to integer, we mention int keyword in the brackets before the decimal double value. The only downside of conversion using typecasting is that the double value is not rounded of, instead the digits after decimal are truncated. We can solve this issue by using Math.round() method which we have discussed in the second example.
Output: Here is the output of the above program –
2. Java – Convert double to int using Math.round()
In this example we are converting the double value to integer value using Math.round() method. This method rounds of the number to nearest integer. As you can see in the output that the double value 99.99 is rounded of to the nearest int value 100 .
Output:
Convert double to int in Java using Double.intValue()
In this example we are using Wrapper class Double . This is same as typecasting method, where the digits after decimal are truncated.
Output:
Top Related Articles:
About the Author
I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.
How to convert double to int in Java? Example
Suppose you have a double primitive variable 4.444 and you want to convert it to the integer value 4 , how do you that in Java? Since double is bigger data type than int , you can simply downcast double to int in Java. double is 64-bit primitive value and when you cast it to a 32-bit integer, anything after the decimal point is lost. Btw, typecasting doesn’t do any rounding or flooring, which means if you have 9.999999 and while casting to int you are expecting 10 then you would be disappointed, casting will give you just 9. If you need 10 then you need to use Math.round() method to first round the double value to the nearest integer and then truncate decimals.
As we have seen, while converting float to int, the Math.round() method is overloaded and the version which accepts double returns a long primitive value. If you need an int primitive value then you need to further cast long to int in Java. By the way, that’s not the only way.
You can also convert a double primitive variable to a Double wrapper object and then call the intValue() method to get a corresponding int value. In this article, I’ll show you a couple of examples to convert double to int in Java.
double to int in Java — Typecasting
The easiest way to convert a double to int in Java is by type casting but it works only when your requirement is just to get rid of anything after the decimal point. Since double is bigger data type than int , it needs to be down-casted as shown below:
int value = (int) 6.14; // 6 int score = (int) 6.99; // 6
If you remember, floating-point numbers are by default double in Java, so you don’t need any suffix, unlike representing float values. As I said, casting gets rid of anything after decimal so even 6.999 gets converted into 6.
double to int — Math.round()
If you want to convert floating-point double value to the nearest int value then you should use the Math.round() method. It accepts a double value and converts into the nearest long value by adding 0.5 and truncating decimal points. Once you got the long value, you can cast it to int again, as shown below:
int a = (int) Math.round(6.14); // 3 int b = (int) Math.round(6.99); // 4 int c = (int) Math.round(6.5); // 4 System.out.printf("double : %f, int : %d %n", 3.14, a); System.out.printf("double : %f, int : %d %n", 3.99, b); System.out.printf("double : %f, int : %d %n", 3.5, c);
As you can see that double value is translated to the nearest integer by using the Math.round() method. You can also join these online Java Programming courses to learn more about rounding in Java.
Double to int in Java — Double.intValue()
There is another way to convert a double value to int in Java, by using wrapper class java.lang.Double method intValue() . You can first use auto-boxing to convert double primitive to Double and then just call intValue() method, this will return an equivalent integer value, as shown below :
Double d = 7.99; // 7 int i = d.intValue(); System.out.printf("Double : %f, int : %d %n", d, i);
This works but it’s like going around the world, first convert primitive to object and then back to primitive. It suits better if you already have a Double object. Btw, this method is similar to casting and doesn’t provide a rounding or flooring facility.
Example : // double to int conversion using casting int value = (int) 6.14; // 6 int score = (int) 6.99; // 6 System.out.printf("double : %f, int : %d %n", 6.14, value); System.out.printf("double : %f, int : %d %n", 6.99, score); // double to int after rounding using Math.round() int a = (int) Math.round(6.14); // 3 int b = (int) Math.round(6.99); // 4 int c = (int) Math.round(6.5); // 4 System.out.printf("double : %f, int : %d %n", 3.14, a); System.out.printf("double : %f, int : %d %n", 3.99, b); System.out.printf("double : %f, int : %d %n", 3.5, c); // Double to int using wrapper class Double d = 7.99; // 7 int i = d.intValue(); System.out.printf("Double : %f, int : %d %n", d, i);
You can see that all the conversion is direct conversion, no rounding or flooring is applied. You can also see these beginner core Java courses to learn more about flooring and rounding in Java.
Important points
1) By default, all floating-point numbers are double in Java, hence you don’t need any prefix, but when you declare a floating-point value with type float, you must use the «f» or «F» as suffix e.g.
2) By down casting a double to an int, you can remove anything after the decimal point. Though this will not apply any rounding or flooring.
3) The Math.round() method from java.lang.Math class converts a double value to the nearest long value, if you want int, you can cast long to int value.
4) You can also convert a double to int in Java by first converting it into an object of the corresponding wrapper class e.g. Double and then calling the intValue() method e.g. Double.intValue()
Here is the summary of all three ways to convert a double to an int value in Java:
That’s all about how to convert double to int in Java. The simplest way to convert double to int in Java is by downcasting it. This will return you the non-fractional part of the number as an int, but it doesn’t do rounding, so even 9.999 will be converted to 9.
If you are looking to convert double to nearest integer e.g. 9.999 to 10 then you should use the Math.random() method. It rounds the double value to the nearest integer by adding 0.5 and then truncating the decimal value. Though, this method return long, which needs to be further downcast into an int.
Another, third way to convert double to int is by using Double.intValue() method, which is best if you already have a Double object, but you can also use auto-boxing to convert double primitive to Double object and then call this method.
- How to convert String to Integer in Java? (tutorial)
- How to convert float to int in Java? (tutorial)
- How to convert String to float in Java? (tutorial)
- The best way to convert Integer to String in Java? (example)
- How to convert a char value to String in Java? (tutorial)
- How to convert String to ASCII values in Java? (example)
- How to convert a long to String in Java? (article)
How to Convert double to int in Java
In Java, “double” and “int” are the most popular primitive data types, where double is a 64-bit floating-point number and integer, also known as “int”, is a 32-bit integer representing signed two’s complement. While programming in Java, you may need to convert double to int where the input given by the user is in floating points, and an integer value is required for further processing.
This post will teach the procedure of converting double to int in Java.
How to Convert double to int in Java?
Converting a number from double to int is considered a common conversion task in programming. As mentioned earlier, the double data type has a decimal value. When these numbers are converted to integers, the decimal digits are rounded to the closest number.
In Java, you can convert data type double to int:
Let’s discuss each of them one by one!
Method 1: Convert double to int in Java by Using TypeCasting
Typecasting is a process of converting one data type to another. It is primarily utilized by Java developers to test the data variable. You can also use it for converting double data type variables to int.
The syntax for converting double to int in Java using TypeCasting method is given as:
“d” is the variable that stores a value of data type “double”. The keyword “int” with the parenthesis like “(int)” indicates that the mentioned double type “d” variable is typecasted into an integer, and the resultant value will be stored in “i”.
First, we will create a double type variable “d” and assign it the following value:
Next, we will convert “d” into an int by using typecasting:
After performing the specified operation, we will print the converted value:
The output shows the integer value “856” after truncating the decimal points:
Method 2: Convert double to int in Java by Using Double.intValue() Method
Double.intValue() is another method used to convert the number from double data type to int. The “intValue()” is the method of the Java “Double” Wrapper class. This method converts the double value into the nearest integer value and returns an integer. It works similar to typecasting.
The syntax for converting a number in data type double to int by using “Double.intValue()” method is as follows:
Have a look at the following example to know more about the given method.
We will convert the value of the already created “d” variable using the “Double.intValue()” method:
The above-given code will create an object of the “Double” Wrapper class and pass a value of “d” in it, and then call the “intValue()” method.
After converting, print the resultant integer value on the console using the “System.out.println()” method:
Method 3: Convert double to int in Java by Using Math.round() Method
Another method to convert the double to int in Java is the “Math.round()”. It belongs to the Java “Math” class. It takes a double value and rounds it into the nearest integer value.
The syntax for the “Math.round()” method is:
We will utilize the same “d” variable and convert its value to “int” using the Math.round() method:
At the end, print the integer value on the console:
You can see the output, which shows the double value “850.171” is rounded off to “850”:
We provided all the necessary instructions related to converting double to int in Java.
Conclusion
To convert double to int in Java, there are three different methods: Typecasting, Math.round(), and Double.intValue(). Typecasting and Double.intValue() methods approximately work the same when utilized for double to int conversion. Typecasting is performed explicitly. In contrast, the Math.round() method rounds off the double value to an integer value. In this post, we have discussed the procedure for converting double to int in Java with examples.
About the author
Farah Batool
I completed my master’s degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.