Double to Int Java
double to int conversion methods
1. double to int in java using typecasting
To convert a double to int in Java typecasting can be used. But there is a problem, it works only if we need to get the number before decimal point. Typecasting excludes, it doesn't consider the digits after decimal point. Sincedouble
is much bigger data type than int
so it needs to be rounded down as given below:
public class doubleToint{
public static void main(String args[]){
double data = 91.99;
int value = (int) data; // 91
System.out.printf("double value: %f, int value : %d %n", data, value);
}
}
/* Output of program:-
double value: 91.990000, int value : 91
*/
In java, the floating point numbers are by default considered as double
, so you don't need any suffix unlike float values. Typecasting a double to int always rounds down so even 91.99 gets converted to 91.
2. round double to int Java
To convert a floating point double number to its nearest int valueMath.round()
method can be used.The built-in math function
java.lang.Math.round()
accepts a double value as a argument and returns the closest long to the argument. The result is rounded to an integer by adding 1/2 then floor of result is taken and cast into type long again. If the argument of type NaN then Math.round()
outputs 0 as result.Now we can cast the long value into int value, to get double to int conversion done.
public class doubleToint{
public static void main(String args[]){
int x = (int) Math.round(91.24);
int y = (int) Math.round(135.99);
System.out.printf("double value: %f, int value : %d %n", 91.24, x);
System.out.printf("double value: %f, int value: %d %n", 135.99, y);
}
}
/* Output of above program:-
double value: 91.240000, int value : 91
double value: 135.990000, int value: 136
*/
3. Double to int using Wrapper Classes
To convert a primitive data typedouble
to int
(integer) we first we need to wrap the double
value with Double
class. This way we make it as object and then we can extract the value from object as integer because all these types belong to Number class, so we can wrap one type of value and get another. intValue() method is used to extract the integer value from Double object.
If you try to cast java.lang.Double
to java.lang.Integer
you will face an exception
"java.lang.ClassCastException: java.lang.Double incompatible with java.lang.Integer"
because Double
is a class and not a primitive data type in java. So we need to wrap double
to Double
as explained above.
public class doubleToint{
public static void main(String args[]){
Double doubleValue = 99.99;
int integerValue = doubleValue.intValue();
System.out.printf("Double value: %f, int value : %d %n", doubleValue, integerValue);
}
}
/* Output of above program:-
Double value: 99.990000, int value : 99
*/
This method does't provide flooring or rounding.
4. double string to integer or long in java
To convert a double value present in the string into an integer or long value we can use Double.parseDouble()
method.
public class stringDoubleTointLong{
public static void main(String args[]){
double data = Double.parseDouble("1.15");
long longValue = (long) data;
int value = (int) data;
System.out.printf("double value: %f, int value : %d long value : %d %n", data, value,longValue);
}
}
/* Output of program:-
double value: 1.150000, int value : 1 long value : 1
*/
Related Links