# Double to Int Java

## 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. Since `double` 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 value `Math.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 type `double` 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 String to int in Java ```
``` { "@context" : "http://schema.org", "@type" : "Article", "name" : "Double to Int Java", "author" : { "@type" : "Person", "name" : "Abhijit Kokane" }, "datePublished" : "2019-07-05", "articleBody" : "<DIV><DIV class=\"border-box bcolorblue\"> <H4 class=\"center\">double to int conversion methods</4>\n<OL>\n<LI><A href=\"#typecasting\">double to int in java using typecasting</A></LI>\n<LI><A href=\"#round-method\">double to int using Math.round()</A></LI>\n<LI><A href=\"#intValue-method\">Double to int using Double.intValue()</A></LI>\n<LI><A href=\"#parseDouble-method\">double string to integer or long in java</A></LI>\n</OL>\n</DIV>\n\n<H2 id=\"typecasting\">1. double to int in java using typecasting</2>\n\nTo 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. Since <CODE>double</CODE> is much bigger data type than <CODE>int</CODE> so it needs to be rounded down as given below:\n\n\n<PRE class=\"prettyprint lang-java\"><CODE>public class doubleToint{\n public static void main(String args[]){\n\t\tdouble data = 91.99;\n\t\tint value = (int) data; // 91 \n\t\tSystem.out.printf(&quot;double value: %f, int value : %d %n&quot;, data, value);\n }\n}\n\n/* Output of program:-\n\ndouble value: 91.990000, int value : 91 \n\n*/</CODE></PRE>\n\n\nIn java, the floating point numbers are by default considered as <CODE>double</CODE>, so you don&#39;t need any suffix unlike float values. Typecasting a double to int always rounds down so even 91.99 gets converted to 91.\n\n\n<H2 id=\"round-method\">2. round double to int Java</2>\n\nTo convert a floating point double number to its nearest int value <CODE>Math.round()</CODE> method can be used.<BR/><BR/>\n\nThe built-in math function <CODE>java.lang.Math.round()</CODE> 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 <CODE>Math.round()</CODE> outputs 0 as result.<BR/><BR/>\n\nNow we can cast the long value into int value, to get double to int conversion done.\n\n<PRE class=\"prettyprint lang-java\"><CODE>public class doubleToint{\n public static void main(String args[]){\n\t\tint x = (int) Math.round(91.24);\n\t\tint y = (int) Math.round(135.99); \n\t\tSystem.out.printf(&quot;double value: %f, int value : %d %n&quot;, 91.24, x);\n\t\tSystem.out.printf(&quot;double value: %f, int value: %d %n&quot;, 135.99, y);\n }\n}\n\n/* Output of above program:-\n\ndouble value: 91.240000, int value : 91 \ndouble value: 135.990000, int value: 136\n\n*/</CODE></PRE>\n\n<H2 id=\"intValue-method\">3. Double to int using Double.intValue()</2>\n\nTo convert a primitive data type <CODE>double</CODE> to <CODE>int</CODE> (integer) we first we need to wrap the <CODE>double</CODE> value with <CODE><B>Double</B></CODE> 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. <CODE><B>intValue()</B></CODE> method is used to extract the integer value from Double object.<BR/><BR/>\n\nIf you try to cast <CODE>java.lang.Double</CODE> to <CODE>java.lang.Integer</CODE> you will face an exception<BR/>\n\n<DIV class=\"note\"><CODE>&quot;java.lang.ClassCastException: java.lang.Double incompatible with java.lang.Integer&quot;</CODE></DIV>\n\nbecause <CODE>Double</CODE> is a class and not a primitive data type in java. So we need to wrap <CODE><B>double</B></CODE> to <CODE><b>Double</b></CODE> as explained above.\n\n<PRE class=\"prettyprint lang-java\"><CODE>public class doubleToint{\n public static void main(String args[]){\n\t\tDouble doubleValue = 99.99;\n\t\tint integerValue = doubleValue.intValue();\n\t\tSystem.out.printf(&quot;Double value: %f, int value : %d %n&quot;, doubleValue, integerValue);\n }\n}\n\n/* Output of above program:-\n\nDouble value: 99.990000, int value : 99 \n\n*/</CODE></PRE>\n\nThis method does&#39;t provide flooring or rounding.\n\n<H2 id=\"parseDouble-method\">4. double string to integer or long in java</2>\n\nTo convert a double value present in the string into an integer or long value we can use <CODE>Double.parseDouble()</CODE> method.\n\n<PRE class=\"prettyprint lang-java\"><CODE>public class stringDoubleTointLong{\n public static void main(String args[]){\n\t\tdouble data = Double.parseDouble(&quot;1.15&quot;);\n\t\tlong longValue = (long) data;\n\t\tint value = (int) data;\n\t\tSystem.out.printf(&quot;double value: %f, int value : %d long value : %d %n&quot;, data, value,longValue);\n }\n}\n\n/* Output of program:-\n\ndouble value: 1.150000, int value : 1 long value : 1 \n\n*/</CODE></PRE><BR/><BR/>\n\n <B>Related Links</B>\n<UL>\n<LI><A href=\"https://www.computerscienceai.com/2019/06/java-convert-string-to-int.html\" class=\"underline2\">String to int in Java</A></LI>\n</UL>\n\n<BR/></DIV>", "headline": "Double to Int Java", "dateModified" : "2019-08-19","image": [ "https://1.bp.blogspot.com/-rHyaJPe5BlY/XRcCyq6bLKI/AAAAAAAABsU/aPkSYyAtn-An4kfnFAiw9ImDS_YHWZ2XACLcBGAs/s1600/Java.png" ], "mainEntityOfPage": { "@type": "WebPage", "@id": "https://www.computerscienceai.com/2019/07/java-double-to-int.html" }, "url" : "https://www.computerscienceai.com/2019/07/java-double-to-int.html", "publisher": { "@type": "Organization", "name": "Computer Science AI", "logo": { "@type": "ImageObject", "url": "https://4.bp.blogspot.com/-k_wHJ93C7_U/W17liXvXBGI/AAAAAAAABS4/uH3Qw95m8BUurcILoCYDT7JgmiFSOc6xwCLcBGAs/s1600/CSALOGO.png" } } } ```
``` ```
``` Get link Facebook Twitter Pinterest Email Other Apps Labels: java ```
``` (adsbygoogle = window.adsbygoogle || []).push({}); (adsbygoogle = window.adsbygoogle || []).push({}); Get link Facebook Twitter Pinterest Email Other Apps Labels java ```
``` ```
``` ```
``` ```
``` ```
``` ```