beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java – Convert double to int example

By Chaitanya Singh | Filed Under: Java Conversion

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.

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

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

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.

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 after conversion
	System.out.println(inum);  
   }
}

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

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.

public class JavaExample{  
   public static void main(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

❮ PreviousNext ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Conversion

  • Java String to int
  • Java int to String
  • Java String to long
  • Java long to String
  • Java String to double
  • Java double to String
  • Java double to int
  • Java int to double
  • Java int to long
  • Java long to int
  • Java char to String
  • Java char to int
  • Java int to char
  • Java float to String
  • Java boolean to String
  • Java String to boolean
  • Java binary to decimal
  • Java decimal to binary
  • Java binary to Octal
  • Java decimal to hexadecimal
  • Java Hex to decimal
  • Java decimal to Octal
  • Java Octal to decimal
  • Java ASCII to String
  • Java Writer to String
  • Java StackTrace to String
  • Java String to ArrayList
  • Java StringBuffer to String
  • Java InputStream to String

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap