Java double to number one

3 ways : Convert double to int in java with example

In this tutorial I will share 3 ways to convert double to int in java. As you know double primitive can contain decimal digits (digits after decimal point). The decimal digits are truncated when we convert double with decimal digits into the int value.If the double value is 9.99 then it will give 9 as int value. Out of the three ways there is one way (Math.round() function) by which you can round of to the nearest integer value i.e int value will be 10 for double value 9.99.

1. Convert double to int using Typecasting : easiest way
2. Convert double to int using Math.round() function : use it to round of double to the nearest integer
3. Convert double to int using Double.intValue() method: use it when you have Double object

1. Convert double to int using Typecasting

In this example I will be converting double to int using typecasting. Typecasting is done by mentioning int in the brackets before the decimal value. The only downside of the typecasting is that it will truncate the decimal digits from the double value. It will not round of to the nearest value. For example, if we have double 99.99 then after typecasting the result will be 99 not 100. If you want 100 as an output then use Math.round() function.

/* Java Program to demonstrate conversion of double to int using Typecasting */ public class JavaHungry  // main method public static void main (String args[])  // given double value double num = 39.99; // converting to int int result = (int) num; // showing the int value System.out.println(result); > > 

2. Convert double to int using Math.round() function

In this example I will be converting double to int using Math.round() function. This function helps by rounding of the double to the nearest integer. For example, if we have double 99.99 then we will get 100 as int value.

/* Java Program to demonstrate conversion of double to int using Math.round() function */ public class JavaHungry  // main method public static void main (String args[])  // given double value double num = 39.99; // converting to int int result = (int) Math.round(num); // showing the int value System.out.println(result); > > 

3. Convert double to int using Double.intValue() method

In this example I will be using the wrapper Double class intValue() method to convert double to int in java. Just like typecasting, this method also truncates digits after decimal. For example, if we have double 99.99 then we will get 99 as int value.

/* Java Program to demonstrate conversion of double to int using Double.intValue() function */ public class JavaHungry  // main method public static void main (String args[])  // given double value double num = 39.99; // Creating a wrapper around // the double value Double doubleObj = new Double(num); // converting to int int result = doubleObj.intValue(); // showing the int value System.out.println(result); > > 

What happens if double is out of range for int value?

One point to note, if the value of the double is negative infinity or any value less than or equal to the Integer.MIN_VALUE, then the result is equal to the Integer.MIN_VALUE.
Similarly,
If value of double is positive infinity or any value greater than or equal to Integer.MAX_VALUE, then the result is equal to the Integer.MAX_VALUE.

/* Java Program to demonstrate conversion of double to int where double is greater than Integer.MAX_VALUE */ public class JavaHungry  // main method public static void main (String args[])  // given double value // larger than Integer.MAX_VALUE double num = 999999999999999999.99; // converting to int int result = (int) num; // showing the int value // It will print the Integer.MAX_VALUE System.out.println(result); > > 

In the above article, I have demonstrated 3 ways to convert double to int in java. Please mention in the comments if you have any questions. I will be happy to help.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry

Источник

How to convert double to int in Java? Example

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.

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.

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 the intValue() method e.g. Double.intValue()

Here is the summary of all three ways to convert a double to an int value in Java:

How to convert double to int 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.

  • 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)

Источник

Java – Convert double to int example

In this tutorial, we will learn how to convert double to int in Java. As we know double value can contain decimal digits (digits after decimal point), so when we convert double value with decimal digits to int value, the decimal digits are truncated. In this java tutorial, we will see three ways to convert a double value to int. Out of these 3 ways there is one way in which you can round of the double value to nearest integer value.

1. Convert double to int using typecasting
2. Convert double to int using Math.round() – This ensures that the decimal digits double value is rounded of to the nearest int value.
3. Convert double to int using Double.intValue()

1. Java – double to int conversion using type casting

To typecast a double value to integer, we mention int keyword in the brackets before the decimal double value. The only downside of conversion using typecasting is that the double value is not rounded of, instead the digits after decimal are truncated. We can solve this issue by using Math.round() method which we have discussed in the second example.

Java Convert double to int using typecasting

Output: Here is the output of the above program –

2. Java – Convert double to int using Math.round()

In this example we are converting the double value to integer value using Math.round() method. This method rounds of the number to nearest integer. As you can see in the output that the double value 99.99 is rounded of to the nearest int value 100 .

Java double to int conversion using math.round() method

Output:

Convert double to int in Java using Double.intValue()

In this example we are using Wrapper class Double . This is same as typecasting method, where the digits after decimal are truncated.

Convert double to int in Java using Double.intValue() method

Output:

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

Источник

Читайте также:  This content requires javascript to be enabled and adobe flash player
Оцените статью