How To Change A Double To An Int In Java
Suppose you accept a double primitive variable 4.444 and you want to convert it to the integer value 4, how exercise you lot that in Java? Since double is bigger data type than int, you tin but downcast double to int in Java. double is 64-flake primitive value and when y'all cast it to a 32-scrap integer, anything after the decimal point is lost. Btw, typecasting doesn't exercise whatever rounding or flooring, which means if you take 9.999999 and while casting to int you lot are expecting 10 then you would be disappointed, casting will requite you just 9. If you need 10 then you demand to apply Math.round() method to first round the double value to the nearest integer and so truncate decimals.
As we accept seen, while converting float to int, the Math.circular() method is overloaded and the version which accepts double returns a long primitive value. If you lot demand an int primitive value so you demand to further cast long to int in Coffee. By the mode, that'due south not the merely way.
Y'all can as well convert a double primitive variable to a Double wrapper object and and then call the intValue() method to get a corresponding int value. In this article, I'll show you a couple of examples to convert double to int in Coffee.
double to int in Java - Typecasting
The easiest fashion to convert a double to int in Java is by type casting but it works simply when your requirement is merely to get rid of anything after the decimal point. Since double is bigger data type than int, information technology needs to be down-casted as shown below:
int value = (int) 6.14; // 6 int score = (int) 6.99; // 6
If you remember, floating-point numbers are by default double in Java, so yous don't need any suffix, different representing float values. As I said, casting gets rid of anything later decimal and then even six.999 gets converted into 6.
double to int - Math.round()
If you desire to catechumen floating-point double value to the nearest int value so you lot should utilise the Math.round() method. It accepts a double value and converts into the nearest long value by adding 0.v and truncating decimal points. Once you got the long value, you can cast it to int again, every bit shown beneath:
int a = (int) Math.round(vi.14); // iii int b = (int) Math.round(six.99); // 4 int c = (int) Math.round(6.five); // four System.out.printf("double : %f, int : %d %n", 3.14, a); System.out.printf("double : %f, int : %d %n", 3.99, b); System.out.printf("double : %f, int : %d %due north", 3.5, c);
As yous tin can see that double value is translated to the nearest integer by using theMath.circular() method. You tin also join these online Java Programming courses to learn more virtually rounding in Java.
Double to int in Java - Double.intValue()
There is another way to convert a double value to int in Coffee, by using wrapper class coffee.lang.Double method intValue(). Yous tin can first use auto-battle to catechumen double primitive to Double and so simply call intValue() method, this will return an equivalent integer value, as shown below :
Double d = 7.99; // 7 int i = d.intValue(); Arrangement.out.printf("Double : %f, int : %d %n", d, i);
This works but it'south similar going around the world, offset convert primitive to object and then back to primitive. It suits better if you already have a Double object. Btw, this method is similar to casting and doesn't provide a rounding or flooring facility.
Example : // double to int conversion using casting int value = (int) vi.14; // 6 int score = (int) 6.99; // half dozen System.out.printf("double : %f, int : %d %n", half dozen.fourteen, value); System.out.printf("double : %f, int : %d %northward", half dozen.99, score); // double to int later on rounding using Math.circular() int a = (int) Math.circular(6.fourteen); // three int b = (int) Math.round(six.99); // four int c = (int) Math.round(6.5); // 4 Organization.out.printf("double : %f, int : %d %n", 3.14, a); Arrangement.out.printf("double : %f, int : %d %n", 3.99, b); System.out.printf("double : %f, int : %d %n", 3.5, c); // Double to int using wrapper class Double d = 7.99; // 7 int i = d.intValue(); Arrangement.out.printf("Double : %f, int : %d %n", d, i);
Y'all can see that all the conversion is direct conversion, no rounding or flooring is applied. You tin besides see these beginner core Java courses to acquire more about flooring and rounding in Java.
Important points
i) By default, all floating-bespeak numbers are double in Java, hence you lot don't demand any prefix, but when yous declare a floating-point value with blazon bladder, y'all must utilize the "f" or "F" every bit suffix e.g.
float PIE = 3.14f;
2) By down casting a double to an int, you can remove annihilation afterward the decimal signal. Though this will not apply whatever rounding or flooring.
iii) The Math.round() method from java.lang.Math class converts a double value to the nearest long value, if you desire int, you can cast long to int value.
4) You can besides convert a double to int in Java by first converting it into an object of the corresponding wrapper grade due east.grand. Double and then calling theintValue() method e.one thousand. Double.intValue()
Here is the summary of all three ways to convert a double to an int value in Java:
That'south all about how to convert double to int in Java. The simplest way to convert double to int in Java is by downcasting it. This will render you the not-fractional office of the number as an int, simply it doesn't practice rounding, so fifty-fifty 9.999 will be converted to ix.
If y'all are looking to convert double to nearest integer e.g. nine.999 to 10 then you should use theMath.random() method. It rounds the double value to the nearest integer past adding 0.five and then truncating the decimal value. Though, this method return long, which needs to exist farther downcast into an int.
Some other, third way to convert double to int is by using Double.intValue() method, which is best if you already have a Double object, but you can also use machine-boxing to catechumen double primitive to Double object and and so call this method.
Other data type conversion articles you may like
- How to convert String to Integer in Coffee? (tutorial)
- How to convert float to int in Coffee? (tutorial)
- How to convert String to bladder in Java? (tutorial)
- The best way to catechumen Integer to String in Java? (case)
- How to catechumen a char value to String in Java? (tutorial)
- How to convert String to ASCII values in Java? (example)
- How to convert a long to String in Coffee? (article)
Thank you for reading this tutorial so far, if you similar then delight share with your friends and colleagues, it makes a lot of deviation.
Source: https://javarevisited.blogspot.com/2017/01/how-to-convert-double-to-int-in-java.html
Posted by: holtvared1955.blogspot.com
0 Response to "How To Change A Double To An Int In Java"
Post a Comment