Конвертировать double to int java

Как 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 > > 

Источник

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

Источник

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

Источник

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.

Источник

Читайте также:  Питон создать копию массива
Оцените статью