Skip to content

Commit f590fd2

Browse files
authored
Merge pull request #5 from coderolls/java-basic/double-to-int
Add java programs to convert double to int in java
2 parents 25f7105 + 193d7b9 commit f590fd2

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
*
3+
* A java program to convert double to int using
4+
* Double.intValue() method
5+
* @author Gaurav Kukade at coderolls.com
6+
*
7+
**/
8+
public class DoubleToIntUsingIntValueMethod{
9+
10+
public static void main(String []args){
11+
12+
double doubleValue = 82.14; // 82.14
13+
14+
System.out.println("doubleValue: "+doubleValue);
15+
16+
//create Double wrapper object
17+
Double doubleValueObject = new Double(doubleValue);
18+
19+
20+
//typecase double to int
21+
int intValue = doubleValueObject.intValue(); // 82
22+
23+
System.out.println("intValue: "+intValue);
24+
}
25+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* A java program to convert double to int using
3+
* Math.round() method
4+
* @author Gaurav Kukade at coderolls.com
5+
**/
6+
public class DoubleToIntUsingRoundMethod{
7+
8+
public static void main(String []args){
9+
10+
// case 1
11+
double doubleValue = 82.14; // 82.14
12+
13+
System.out.println("doubleValue: "+doubleValue);
14+
15+
//typecase double to int
16+
int intValue = (int) Math.round(doubleValue); // 82
17+
18+
System.out.println("intValue: "+intValue);
19+
20+
System.out.println();
21+
22+
// case 2
23+
double nextDoubleValue = 82.99; //
24+
25+
26+
System.out.println("nextDoubleValue: "+nextDoubleValue);
27+
28+
// Math.round(nextDoubleValue) returns long value
29+
//typecase long to int
30+
int nextIntValue = (int) Math.round(nextDoubleValue); // 83
31+
32+
System.out.println("nextIntValue: "+nextIntValue);
33+
}
34+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* A java program to convert double to int using typecasting
3+
* @author Gaurav Kukade at coderolls.com
4+
**/
5+
public class DoubleToIntUsingTypecasting{
6+
7+
public static void main(String []args){
8+
9+
double doubleValue = 82.14; // 82.14
10+
11+
System.out.println("doubleValue: "+doubleValue);
12+
13+
//typecase double to int
14+
int intValue = (int) doubleValue; // 82
15+
16+
System.out.println("intValue: "+intValue);
17+
}
18+
}

0 commit comments

Comments
 (0)