BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl
Home / java / Java – Convert double to int example

Java – Convert double to int example

By Chaitanya Singh

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 ❯

Posted Under: java | Tags: Java-Conversion

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap