Suppose you have a double primitive variable 4.444 and you want to convert it to the integer value 4, how do you that in Java? Since double is bigger data type than int, you can simply downcast double to int in Java. double is 64-bit primitive value and when you cast it to a 32-bit integer, anything after the decimal point is lost. Btw, typecasting doesn't do any rounding or flooring, which means if you have 9.999999 and while casting to int you are expecting 10 then you would be disappointed, casting will give you just 9. If you need 10 then you need to use Math.round() method to first round the double value to the nearest integer and then truncate decimals.
As we have seen, while converting float to int, the Math.round() method is overloaded and the version which accepts double returns a long primitive value. If you need an int primitive value then you need to further cast long to int in Java. By the way, that's not the only way.
You can also convert a double primitive variable to a Double wrapper object 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 Java.
If you remember, floating-point numbers are by default double in Java, so you don't need any suffix, unlike representing float values. As I said, casting gets rid of anything after decimal so even 6.999 gets converted into 6.
As you can see that double value is translated to the nearest integer by using the Math.round() method. You can also join these online Java Programming courses to learn more about rounding in Java.
This works but it's like going around the world, first 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.
You can see that all the conversion is direct conversion, no rounding or flooring is applied. You can also see these beginner core Java courses to learn more about flooring and rounding in Java.
float PIE = 3.14f;
2) By down casting a double to an int, you can remove anything after the decimal point. Though this will not apply any rounding or flooring.
3) The Math.round() method from java.lang.Math class converts a double value to the nearest long value, if you want int, you can cast long to int value.
4) You can also convert a double to int in Java by first converting it into an object of the corresponding wrapper class e.g. Double and then calling theintValue() method e.g. Double.intValue()
Here is the summary of all three ways to convert a double to an int value in Java:
That's 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 return you the non-fractional part of the number as an int, but it doesn't do rounding, so even 9.999 will be converted to 9.
If you are looking to convert double to nearest integer e.g. 9.999 to 10 then you should use the Math.random() method. It rounds the double value to the nearest integer by adding 0.5 and then truncating the decimal value. Though, this method return long, which needs to be further downcast into an int.
Another, 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 auto-boxing to convert double primitive to Double object and then call this method.
Other data type conversion articles you may like
As we have seen, while converting float to int, the Math.round() method is overloaded and the version which accepts double returns a long primitive value. If you need an int primitive value then you need to further cast long to int in Java. By the way, that's not the only way.
You can also convert a double primitive variable to a Double wrapper object 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 Java.
double to int in Java - Typecasting
The easiest way to convert a double to int in Java is by type casting but it works only when your requirement is just to get rid of anything after the decimal point. Since double is bigger data type than int, it 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 you don't need any suffix, unlike representing float values. As I said, casting gets rid of anything after decimal so even 6.999 gets converted into 6.
double to int - Math.round()
If you want to convert floating-point double value to the nearest int value then you should use the Math.round() method. It accepts a double value and converts into the nearest long value by adding 0.5 and truncating decimal points. Once you got the long value, you can cast it to int again, as shown below:int a = (int) Math.round(6.14); // 3 int b = (int) Math.round(6.99); // 4 int c = (int) Math.round(6.5); // 4 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 %n", 3.5, c);
As you can see that double value is translated to the nearest integer by using the Math.round() method. You can also join these online Java Programming courses to learn more about rounding in Java.
Double to int in Java - Double.intValue()
There is another way to convert a double value to int in Java, by using wrapper class java.lang.Double method intValue(). You can first use auto-boxing to convert double primitive to Double and then just call intValue() method, this will return an equivalent integer value, as shown below :Double d = 7.99; // 7 int i = d.intValue(); System.out.printf("Double : %f, int : %d %n", d, i);
This works but it's like going around the world, first 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) 6.14; // 6 int score = (int) 6.99; // 6 System.out.printf("double : %f, int : %d %n", 6.14, value); System.out.printf("double : %f, int : %d %n", 6.99, score); // double to int after rounding using Math.round() int a = (int) Math.round(6.14); // 3 int b = (int) Math.round(6.99); // 4 int c = (int) Math.round(6.5); // 4 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 %n", 3.5, c); // Double to int using wrapper class Double d = 7.99; // 7 int i = d.intValue(); System.out.printf("Double : %f, int : %d %n", d, i);
You can see that all the conversion is direct conversion, no rounding or flooring is applied. You can also see these beginner core Java courses to learn more about flooring and rounding in Java.
Important points
1) By default, all floating-point numbers are double in Java, hence you don't need any prefix, but when you declare a floating-point value with type float, you must use the "f" or "F" as suffix e.g.float PIE = 3.14f;
2) By down casting a double to an int, you can remove anything after the decimal point. Though this will not apply any rounding or flooring.
3) The Math.round() method from java.lang.Math class converts a double value to the nearest long value, if you want int, you can cast long to int value.
4) You can also convert a double to int in Java by first converting it into an object of the corresponding wrapper class e.g. Double and then calling theintValue() method e.g. Double.intValue()
Here is the summary of all three ways to convert a double to an int value in Java:
That's 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 return you the non-fractional part of the number as an int, but it doesn't do rounding, so even 9.999 will be converted to 9.
If you are looking to convert double to nearest integer e.g. 9.999 to 10 then you should use the Math.random() method. It rounds the double value to the nearest integer by adding 0.5 and then truncating the decimal value. Though, this method return long, which needs to be further downcast into an int.
Another, 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 auto-boxing to convert double primitive to Double object and then call this method.
Other data type conversion articles you may like
- How to convert String to Integer in Java? (tutorial)
- How to convert float to int in Java? (tutorial)
- How to convert String to float in Java? (tutorial)
- The best way to convert Integer to String in Java? (example)
- How to convert 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 Java? (article)
6 comments :
There is an error in values when using Math.round(), it's just a typo. Values should be 3.xx or 6.xx. Great work btw!
What happens if double is larger than int_max
Also I think typos you mean 6 instead of 3
In last paragraph you have written Math.random() , I think its a typo, it should be Math.round() as you have mentioned in above paragraph.
I hope you are not trusting articles from this source to gain correct understanding of how Java works. Here's just one example of nonsense from the article: "Since double is bigger data type than int, you can simply downcast double to int in Java. double is 64-bit primitive value and when you cast it to 32-bit integer, anything after the decimal point is lost."
1) Double is a "bigger" data type than int, but that has nothing to do with why you are able to cast from double to int.
2) It's not a downcast. A downcast involves an inheritance relationship, a reference to a parent class, and a runtime check/cast to a child class.
3) It's not as simple as "everything after the decimal point is lost". This entire explanation is implying that 32 bits worth of data are simply "chopped off" the double to make an int. Floating point values are stored in a very different way that requires actual calculation to convert it to an int.
At the end of this post, you mentioned Math.random(). Do you mean Math.round()? I
When i do this using wrapper method it give me warning that double cannot be dereferenced.
Post a Comment