Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4b381d0

Browse files
committed
Add blogpost - convert double to int in java
1 parent 2430e0f commit 4b381d0

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
---
2+
layout: post
3+
title: "How To Convert double To int In Java?"
4+
author: gaurav
5+
image: assets/images/2021-03-02/double-to-int-in-java.webp
6+
categories: [ Java, Core Java, String]
7+
description: "In this article you are going to learn how can we convert a double value to an integer value."
8+
---
9+
10+
11+
In this article, we will see how we can convert a double to an int.
12+
13+
In java programming, you will have a double primitive value (ex 82.14), but to do the further operations you need an int value (ex. 82) so let's see how to convert double to int in java.
14+
15+
There are three ways you can convert double to int. I will list them all below and, then we will see them one by one.
16+
17+
1. convert double to int - using typecasting
18+
2. convert double to int - using `Math.round()`
19+
3. convert double to int - using `Double.IntValue()`
20+
21+
> You may like to visit:
22+
> [How To Convert An Integer To String In Java](https://coderolls.com/convert-int-to-string/)
23+
24+
## 1. convert double to int - using typecasting
25+
26+
We know `double` is a 64-bit primitive value, and int is a 32-bit primitive value. So, to convert double to int, we can downcast the double value to int.
27+
28+
I have given a simple example below that shows to convert double to int using typecasting.
29+
30+
```java
31+
/**
32+
* A java program to convert double to int using typecasting
33+
* @author Gaurav Kukade at coderolls.com
34+
**/
35+
public class DoubleToIntUsingTypecasting{
36+
37+
public static void main(String []args){
38+
39+
double doubleValue = 82.14; // 82.14
40+
41+
System.out.println("doubleValue: "+doubleValue);
42+
43+
//typecase double to int
44+
int intValue = (int) doubleValue; // 82
45+
46+
System.out.println("intValue: "+intValue);
47+
}
48+
}
49+
```
50+
Output:
51+
```java
52+
doubleValue: 82.14
53+
intValue: 82
54+
```
55+
56+
The problem with the typecasting is that it will truncate the value after the decimal point. It will not round it.
57+
58+
In the case of 82.14, we will get an int value of 82, which looks ok. But when we have a double value like 82.99, we will get only 82 and loss the 0.99 that is ~1.
59+
60+
It may create an issue in your calculations.
61+
62+
In the case of 82.99, it should be rounded to 83 and then converted to int.
63+
64+
It is not possible with typecasting, but our next solution can achieve it.
65+
66+
## 2. convert double to int - using `Math.round()`
67+
68+
`Math.round()` method will round the floating-point value to the nearest long value. Then we can typecast it to the int.
69+
70+
I have given a simple java program below that shows how to convert double to int using the `Math.round()` method.
71+
72+
```java
73+
/**
74+
* A java program to convert double to int using
75+
* Math.round() method
76+
* @author Gaurav Kukade at coderolls.com
77+
**/
78+
public class DoubleToIntUsingRoundMethod{
79+
80+
public static void main(String []args){
81+
82+
// case 1
83+
double doubleValue = 82.14; // 82.14
84+
85+
System.out.println("doubleValue: "+doubleValue);
86+
87+
//typecase double to int
88+
int intValue = (int) Math.round(doubleValue); // 82
89+
90+
System.out.println("intValue: "+intValue);
91+
92+
System.out.println();
93+
94+
// case 2
95+
double nextDoubleValue = 82.99; //
96+
97+
98+
System.out.println("nextDoubleValue: "+nextDoubleValue);
99+
100+
// Math.round(nextDoubleValue) returns long value
101+
//typecase long to int
102+
int nextIntValue = (int) Math.round(nextDoubleValue); // 83
103+
104+
System.out.println("nextIntValue: "+nextIntValue);
105+
}
106+
}
107+
```
108+
Output:
109+
110+
```java
111+
doubleValue: 82.14
112+
intValue: 82
113+
114+
nextDoubleValue: 82.99
115+
nextIntValue: 83
116+
```
117+
## 3. convert double to int - using `Double.intValue()`
118+
119+
120+
In this way, we will convert the double primitive value to the `Double` wrapper class, and then we can use the `intValue()` method of the `Double` wrapper class.
121+
122+
This method does not round the value before converting it to the int value. It will remove the digits after the decimal point.
123+
124+
I have given a simple java program below that shows how to convert double to int using the `Double.IntValue()` method.
125+
126+
```java
127+
/**
128+
*
129+
* A java program to convert double to int using
130+
* Double.intValue() method
131+
* @author Gaurav Kukade at coderolls.com
132+
*
133+
**/
134+
public class DoubleToIntUsingIntValueMethod{
135+
136+
public static void main(String []args){
137+
138+
double doubleValue = 82.14; // 82.14
139+
140+
System.out.println("doubleValue: "+doubleValue);
141+
142+
//create Double wrapper object
143+
Double doubleValueObject = new Double(doubleValue);
144+
145+
146+
//typecase double to int
147+
int intValue = doubleValueObject.intValue(); // 82
148+
149+
System.out.println("intValue: "+intValue);
150+
}
151+
}
152+
```
153+
154+
Output:
155+
```java
156+
doubleValue: 82.14
157+
intValue: 82
158+
```
159+
## Conclusion
160+
161+
We can convert double to int in java using the three ways given below.
162+
163+
**1. convert double to int - using typecasting**
164+
165+
In this method we typecast the double value to int as give below,
166+
```java
167+
int intValue = (int) Math.round(doubleValue);
168+
```
169+
But in this way, we will lose the value after the decimal point. It will not do the rounding before converting double to int.
170+
171+
**2. convert double to int - using `Math.round()`**
172+
173+
174+
In this way, we use the `Math.round()` method for the rounding purpose.
175+
176+
`Math.round()` method round the double value to the nearest long, and then we can typecast long to the int as given below.
177+
```java
178+
int nextIntValue = (int) Math.round(nextDoubleValue);
179+
```
180+
**3. convert double to int - using `Double.IntValue()`**
181+
182+
In this way, we convert the `double` value to the `Double` wrapper class, and then we use the `Double.intValue()` method to get the int value.
183+
184+
```java
185+
//create Double wrapper object
186+
Double doubleValueObject = new Double(doubleValue);
187+
188+
//typecase double to int
189+
int intValue = doubleValueObject.intValue();
190+
```
191+
In this way also we will lose the digits after the decimal points.
192+
193+
So this is how we do convert a double to int in java. You can check all three ([DoubleToIntUsingTypecasting.java](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-basic/DoubleToIntUsingTypecasting.java), [DoubleToIntUsingRoundMethod.java](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-basic/DoubleToIntUsingRoundMethod.java), [DoubleToIntUsingIntValueMethod.java](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-basic/DoubleToIntUsingIntValueMethod.java)) programs on GitHub
194+
195+
You can read more about [string to int](https://coderolls.com/convert-int-to-string/) and [int to string](https://coderolls.com/convert-string-to-int/) conversion.
196+
197+
-------
198+
You can visit my [YouTube channel 'coderolls'](https://www.youtube.com/channel/UCl31HHUdQbSHOQfc9L-wo3w?view_as=subscriber?sub_confirmation=1) to find more video tutorials.
199+
200+
If you found this article worth, support me by [giving a cup of Coffee ☕](https://www.paypal.me/GauravKukade)
201+
202+
### Related Articles
203+
204+
- [How To Convert An Integer To String In Java](https://coderolls.com/convert-int-to-string/)
205+
206+
- [How to convert String to Integer in Java](https://coderolls.com/convert-string-to-int/)
207+
208+
- [How To Convert StringBuilder To String In Java?](https://coderolls.com/convert-stringbuilder-to-string-in-java/)
209+
210+
- [How Do I Compare Strings In Java](https://coderolls.com/compare-strings-in-java/)
211+
212+
- [How To Reverse A String In Java (5 ways)](https://coderolls.com/reverse-a-string-in-java/)
213+
214+
215+
216+
Binary file not shown.

0 commit comments

Comments
 (0)