You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/java-basic/2021-03-02-convert-double-to-int.md
+10-10Lines changed: 10 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -63,11 +63,11 @@ In the case of 82.99, it should be rounded to 83 and then converted to int.
63
63
64
64
It is not possible with typecasting, but our next solution can achieve it.
65
65
66
-
## 2. convert double to int - using `Math.round()`
66
+
## 2. convert double to int - using [`Math.round()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#round-double-)
67
67
68
-
`Math.round()` method will round the floating-point value to the nearest long value. Then we can typecast it to the int.
68
+
[`Math.round()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#round-double-) method will round the floating-point value to the nearest long value. Then we can typecast it to the int.
69
69
70
-
I have given a simple java program below that shows how to convert double to int using the `Math.round()` method.
70
+
I have given a simple java program below that shows how to convert double to int using the [`Math.round()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#round-double-) method.
71
71
72
72
```java
73
73
/**
@@ -114,14 +114,14 @@ intValue: 82
114
114
nextDoubleValue:82.99
115
115
nextIntValue:83
116
116
```
117
-
## 3. convert double to int - using `Double.intValue()`
117
+
## 3. convert double to int - using [`Double.IntValue()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#intValue--)
118
118
119
119
120
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
121
122
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
123
124
-
I have given a simple java program below that shows how to convert double to int using the `Double.IntValue()` method.
124
+
I have given a simple java program below that shows how to convert double to int using the [`Double.IntValue()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#intValue--) method.
125
125
126
126
```java
127
127
/**
@@ -168,18 +168,18 @@ int intValue = (int) Math.round(doubleValue);
168
168
```
169
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
170
171
-
**2. convert double to int - using `Math.round()`**
171
+
**2. convert double to int - using [`Math.round()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#round-double-)**
172
172
173
173
174
-
In this way, we use the `Math.round()` method for the rounding purpose.
174
+
In this way, we use the [`Math.round()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#round-double-) method for the rounding purpose.
175
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.
176
+
[`Math.round()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#round-double-) method round the double value to the nearest long, and then we can typecast long to the int as given below.
177
177
```java
178
178
int nextIntValue = (int) Math.round(nextDoubleValue);
179
179
```
180
-
**3. convert double to int - using `Double.IntValue()`**
180
+
**3. convert double to int - using [`Double.IntValue()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#intValue--)**
181
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.
182
+
In this way, we convert the `double` value to the `Double` wrapper class, and then we use the [`Double.IntValue()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#intValue--) method to get the int value.
0 commit comments