Skip to content

Add java programs to convert double to int in java #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions java-basic/DoubleToIntUsingIntValueMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
*
* A java program to convert double to int using
* Double.intValue() method
* @author Gaurav Kukade at coderolls.com
*
**/
public class DoubleToIntUsingIntValueMethod{

public static void main(String []args){

double doubleValue = 82.14; // 82.14

System.out.println("doubleValue: "+doubleValue);

//create Double wrapper object
Double doubleValueObject = new Double(doubleValue);


//typecase double to int
int intValue = doubleValueObject.intValue(); // 82

System.out.println("intValue: "+intValue);
}
}
34 changes: 34 additions & 0 deletions java-basic/DoubleToIntUsingRoundMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* A java program to convert double to int using
* Math.round() method
* @author Gaurav Kukade at coderolls.com
**/
public class DoubleToIntUsingRoundMethod{

public static void main(String []args){

// case 1
double doubleValue = 82.14; // 82.14

System.out.println("doubleValue: "+doubleValue);

//typecase double to int
int intValue = (int) Math.round(doubleValue); // 82

System.out.println("intValue: "+intValue);

System.out.println();

// case 2
double nextDoubleValue = 82.99; //


System.out.println("nextDoubleValue: "+nextDoubleValue);

// Math.round(nextDoubleValue) returns long value
//typecase long to int
int nextIntValue = (int) Math.round(nextDoubleValue); // 83

System.out.println("nextIntValue: "+nextIntValue);
}
}
18 changes: 18 additions & 0 deletions java-basic/DoubleToIntUsingTypecasting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* A java program to convert double to int using typecasting
* @author Gaurav Kukade at coderolls.com
**/
public class DoubleToIntUsingTypecasting{

public static void main(String []args){

double doubleValue = 82.14; // 82.14

System.out.println("doubleValue: "+doubleValue);

//typecase double to int
int intValue = (int) doubleValue; // 82

System.out.println("intValue: "+intValue);
}
}