Double to int rounding java

How To Convert double To int In Java?

We know double is a 64-bit primitive value, and int is a 32-bit primitive value. So, to convert double to int, we can downcast the double value to int.

I have given a simple example below that shows to convert double to int using typecasting.

/** * A java program to convert double to int using typecasting * @author Gaurav Kukade at coderolls.com **/ public class DoubleToIntUsingTypecasting public static void main(String []args) double doubleValue = 82.14; // 82.14 System.out.println("doubleValue: "+doubleValue); //typecase double to int int intValue = (int) doubleValue; // 82 System.out.println("intValue: "+intValue); > > 
doubleValue: 82.14 intValue: 82 

The problem with the typecasting is that it will truncate the value after the decimal point. It will not round it.

In the case of 82.14, we will get an int value of 82, which looks ok. But when we have a double value like 82.99, we will get only 82 and loss the 0.99 that is ~1.

It may create an issue in your calculations.

In the case of 82.99, it should be rounded to 83 and then converted to int.

It is not possible with typecasting, but our next solution can achieve it.

2. convert double to int — using Math.round()

Math.round() method will round the floating-point value to the nearest long value. Then we can typecast it to the int.

I have given a simple java program below that shows how to convert double to int using the Math.round() method.

/** * A java program to convert double to int using * Math.round() method * @author Gaurav Kukade at coderolls.com **/ public class DoubleToIntUsingRoundMethod public static void main(String []args) // case 1 double doubleValue = 82.14; // 82.14 System.out.println("doubleValue: "+doubleValue); //typecase double to int int intValue = (int) Math.round(doubleValue); // 82 System.out.println("intValue: "+intValue); System.out.println(); // case 2 double nextDoubleValue = 82.99; // System.out.println("nextDoubleValue: "+nextDoubleValue); // Math.round(nextDoubleValue) returns long value //typecase long to int int nextIntValue = (int) Math.round(nextDoubleValue); // 83 System.out.println("nextIntValue: "+nextIntValue); > > 
doubleValue: 82.14 intValue: 82 nextDoubleValue: 82.99 nextIntValue: 83 

3. convert double to int — using Double.intValue()

In this way, we will convert the double primitive value to the Double wrapper class, and then we can use the intValue() method of the Double wrapper class.

This method does not round the value before converting it to the int value. It will remove the digits after the decimal point.

I have given a simple java program below that shows how to convert double to int using the Double.IntValue() method.

/** * * A java program to convert double to int using * Double.intValue() method * @author Gaurav Kukade at coderolls.com * **/ public class DoubleToIntUsingIntValueMethod public static void main(String []args) double doubleValue = 82.14; // 82.14 System.out.println("doubleValue: "+doubleValue); //create Double wrapper object Double doubleValueObject = new Double(doubleValue); //typecase double to int int intValue = doubleValueObject.intValue(); // 82 System.out.println("intValue: "+intValue); > > 
doubleValue: 82.14 intValue: 82 

Источник

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)

Источник

How to convert a double to int in Java

Many candidates are rejected or down-leveled in technical interviews due to poor performance in behavioral or cultural fit interviews. Ace your interviews with this free course, where you will practice confidently tackling behavioral interview questions.

Let’s say that the double variable x holds the value 3.6987 and needs to be converted to an int . There are two ways that this can be done:

  • All the digits after the decimal are lost and x holds the integer 3 3 3 ; this can be done using typecasting in Java.
  • The value is rounded off to the nearest integer (i.e.,3.6987 is rounded off to 4 4 4 ); this can be done using the Math.round() function in Java.​

1. Typecasting

Since double is a bigger data type than int , it needs to be down-casted. See the syntax below:

int IntValue = (int) DoubleValue; 

Code

The code snippet below illustrates double to int typecasting in Java:

class DoubleToInt
public static void main( String args[] )
double DoubleValue = 3.6987;
int IntValue = (int) DoubleValue;
System.out.println(DoubleValue + " is now " + IntValue);
>
>

2. Using Math.round()

Math.round() accepts a double value and converts it into the nearest long value by adding 0.5 0.5 0.5 to the value and truncating its decimal points. The long value can then be converted to an int using typecasting.

The syntax for the Math.round() function is:

long Math.round(double DoubleValue); 

The code snippet below illustrates the usage of Math.round() when converting a double data type to an int in Java:

Источник

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.

Источник

Читайте также:  Http get запрос на java
Оцените статью