Double value to int in java

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.

Источник

Как double преобразовать в int java

Примитивный тип double можно преобразовать в int явно указав на необходимость преобразования (int) .

Объект Double можно преобразовать с помощью метода intValue() .

Кроме того мы можем использовать методы округления библиотеки Math .

public class App  public static void main(String[] args)  double f = 5.5; int i = (int) f; System.out.println(i); // => 5 Double d = 5.5; int n = d.intValue(); System.out.println(n); // => 5 // округлим до ближайшего целого числа вверх n = (int) Math.ceil(d); System.out.println(n); // => 6 // округлим до ближайшего целого числа вниз n = (int) Math.floor(d); System.out.println(n); // => 5 // округлим до ближайшего целого числа n = (int) Math.round(d); System.out.println(n); // => 6 > > 

Источник

How to Convert Double to Int in Java

In Java, double is a data type that represents floating-point numbers, whereas int is a data type that represents integers. Sometimes, it may be necessary to convert a double to an int, such as when we need to perform arithmetic operations that only work with integers or when we want to store the result in an int variable. This article explores various methods to convert double to int in Java. So, without any further delay, let’s move to our section which is different approaches to convert double to int in Java.

Approaches to Convert double to int in Java

Now, let’s discuss in detail all the approaches to convert double to int in Java:

Approach 1: Using TypeCasting in Java

Type Casting in Java is the simplest way to convert double to int in Java. It involves explicitly converting a data type to another data type using the syntax (datatype) variable. When converting a double to an int using type casting, the decimal part of the double value is truncated, i.e., it is simply removed.

Syntax of TypeCasting in Java

double data = 52.6345 int value = (int)data;

Now, let’s look at an example that converts double to int in Java using TypeCasting.

Code Implementation:

Double - 52.6345 Integer - 52

Explanation: In the above Java example, we declare a class named «PrepBytes» with a «main» method. The «main» method starts by creating a double variable «data» and assigning it the value 52.6345. Next, our Java program converts the double variable «data» to an integer variable «value». After that, we print the value of «value» to the console.

Approach 2: Using Math.round() Method

When we use the Math.round() method to convert a double to int in Java, the decimal part of the double value is rounded to the nearest long value by adding 0.5 and trimming the decimal points. Typecasting can then be used to transform the long value to an int.

Syntax of Math.round() Method

double doubleValue = 52.6345; int intValue = (int) Math.round(doubleValue);

Now, let’s look at an example that convert double to int in Java using Math.round() method.

Code Implementation:

Explanation: In the above example, we declare a class named «PrepBytes» with a «main» method. The «main» method begins by creating a double variable named «data1» and assigning it the value of 52.6345. Next, the program uses the Math.round() method to round the double value of «data1» to the nearest integer value. The rounded value is then explicitly cast to an integer variable «value1» using an explicit cast.

Approach 3: Using Double.intValue() Method

The Double class in Java provides a method called intValue() that returns the int value of a Double object. We can use this method to convert a double value to an int value. When we use the Double.intValue() method to convert a double to an int, the decimal part of the double value is truncated, i.e., it is simply removed.

Syntax of Double.inValue() method

double data = 52.6345 Double newData = new Double(data); int value = newData.intValue();

Now, let’s look at an example that converts double to int in Java using the Double.intValue() method.

Double - 52.6345 Double - 52

Explanation: In the above example, we created a class named «PrepBytes» with a «main» method. The «main» method starts by creating a Double variable «data» and assigning it the value 52.6345. Next, the program creates a new Double object «newData» by passing «data» as a parameter to the constructor of the Double class. The program then converts the Double object «newData» to an integer value using the intValue() method, which returns the integer value of the Double object.

Conclusion
There are several ways to convert a double to int in Java. Type casting, using the Math.round() method, and using the Double.intValue() method are the most commonly used methods. Type casting is the simplest method, while the Math.round() method is useful when we need to round off the decimal part of the double value to the nearest integer. The Double.intValue() method is useful when we need to simply truncate the decimal part of the double value. By understanding these methods, we can easily convert a double to int in our Java programs.

FAQs

Here are some frequently asked questions on how to convert double to int in Java.

Q1: What is the simplest way to convert double to int in Java?
Answer: The simplest way to convert double to int in Java is by using type casting, which involves explicitly converting a data type to another data type using the syntax (datatype) variable.

Q2: What happens when we use type casting to convert double value that is too large to fit to int in Java?
Answer: When we use type casting to convert a double value that is too large to fit into an int in Java, the result is undefined and may lead to an overflow or underflow exception.

Q3: What is the advantage of using the Math.round() method over type casting when trying to convert double to int in Java?
Answer: The advantage of using the Math.round() method over type casting when trying to convert double to int in Java is that it allows us to round off the decimal part of the double value to the nearest integer.

Q4: What is the Double class in Java used for?
Answer: The Double class in Java is a wrapper class that provides methods and constants for working with double values.

Q5: What is the advantage of using the Double.intValue() method over type casting when trying to convert double to int in Java?
Answer: The advantage of using the Double.intValue() method over type casting when trying to convert double to int in Java is that it allows us to work with Double objects and provides more flexibility when working with double values.

Источник

Читайте также:  Java annotation multiple annotations
Оцените статью