banner



How To Change Double To Int In Java

In this tutorial, we will learn how to catechumen 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 in that location is one style in which you can round of the double value to nearest integer value.

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

1. Java – double to int conversion using blazon casting

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

public class JavaExample{      public static void main(String args[]){   	//double value 	double dnum = 99.99; 		 	//catechumen double to int using typecasting 	int inum=(int)dnum;   		 	//displaying int value after conversion 	System.out.println(inum);      } }

Output: Hither is the output of the above program –
Java Convert double to int using typecasting

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

In this instance we are converting the double value to integer value using Math.round() method. This method rounds of the number to nearest integer. Equally y'all can run across in the output that the double value 99.99 is rounded of to the nearest int value 100.

public class JavaExample{      public static void main(String args[]){   	//double value 	double dnum = 99.99; 		 	/* convert double to int using Math.round() 	 * This method rounds of the double value to  	 * nearest integer value. 	 */ 	int inum=(int) Math.round(dnum);   		 	//displaying int value later conversion 	System.out.println(inum);      } }

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

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

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

public class JavaExample{      public static void chief(String args[]){   	Double dnum = 99.99; 	int inum = dnum.intValue();   	System.out.println(inum);      } }

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

Source: https://beginnersbook.com/2018/09/java-convert-double-to-int/

Posted by: benoitcabol2001.blogspot.com

0 Response to "How To Change Double To Int In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel