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

Commit dac345c

Browse files
authored
Merge pull request #21 from coderolls/blogpost/double-to-int
add links to the blogpost
2 parents 215b9e7 + 87f1c04 commit dac345c

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

_posts/java-basic/2021-03-02-convert-double-to-int.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ In the case of 82.99, it should be rounded to 83 and then converted to int.
6363

6464
It is not possible with typecasting, but our next solution can achieve it.
6565

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-)
6767

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.
6969

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.
7171

7272
```java
7373
/**
@@ -114,14 +114,14 @@ intValue: 82
114114
nextDoubleValue: 82.99
115115
nextIntValue: 83
116116
```
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--)
118118

119119

120120
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.
121121

122122
This method does not round the value before converting it to the int value. It will remove the digits after the decimal point.
123123

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.
125125

126126
```java
127127
/**
@@ -168,18 +168,18 @@ int intValue = (int) Math.round(doubleValue);
168168
```
169169
But in this way, we will lose the value after the decimal point. It will not do the rounding before converting double to int.
170170

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-) **
172172

173173

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.
175175

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.
177177
```java
178178
int nextIntValue = (int) Math.round(nextDoubleValue);
179179
```
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--)**
181181

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.
183183

184184
```java
185185
//create Double wrapper object

0 commit comments

Comments
 (0)