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

Java double to int Conversion

Last Updated: June 10, 2024 by Chaitanya Singh | Filed Under: java

In this tutorial, you will learn how to convert double to int in Java. A double number contains decimal digits so when we convert it into an int, the decimal digits are truncated. We can however use certain approaches where we can convert it into a nearest int rather than truncating decimal digits. Let’s see the most common methods:

1. Type Casting

Type casting is the easiest method for double to int conversion. It truncates the decimal part and keeps the integer part. If you only care about the integer part then this is the simplest solution.

double myDouble = 9.99;
int myInt = (int) myDouble;
System.out.println(myInt); // Output: 9

2. Using Math.round()

If you want the nearest integer as output, you can use Math.round() method. This method returns a long value so you need to cast it to convert it into an int.

double myDouble = 9.99;
int myInt = (int) Math.round(myDouble);
System.out.println(myInt); // Output: 10

3. Using Math.floor() and Math.ceil()

You can use Math.floor() or Math.ceil() method to round down or up the result respectively. These methods return a double value so you need to cast the returned value.

double myDouble = 9.99;
int myIntDown = (int) Math.floor(myDouble); //round down
int myIntUp = (int) Math.ceil(myDouble); // round up
System.out.println(myIntDown); // Output: 9
System.out.println(myIntUp); // Output: 10

4. Using Double.intValue()

This method works similar to the Type casting method. It truncates the decimal part and returns the integer part.

Double myDouble = 9.99;
int myInt = myDouble.intValue();
System.out.println(myInt); // Output: 9

Java Program for double to int Conversion

In this program, we will see the discussed methods:

public class Main {
public static void main(String[] args) {
double myDouble = 9.99;

// 1. Type Casting
int myInt1 = (int) myDouble;
System.out.println("Type Casting: " + myInt1);

// 2. Conversion using Math.round()
int myInt2 = (int) Math.round(myDouble);
System.out.println("Math.round(): " + myInt2);

// 3. Round down/up using Math.floor() and Math.ceil()
int myInt3 = (int) Math.floor(myDouble);
int myInt4 = (int) Math.ceil(myDouble);
System.out.println("Math.floor(): " + myInt3);
System.out.println("Math.ceil(): " + myInt4);

// 4. Conversion using Double.intValue()
Double myDoubleObject = myDouble;
int myInt5 = myDoubleObject.intValue();
System.out.println("Double.intValue(): " + myInt5);
}
}

Output:

Type Casting: 9
Math.round(): 10
Math.floor(): 9
Math.ceil(): 10
Double.intValue(): 9

Recommended Posts:

  • Java int to double Conversion
  • Java double to String Conversion

Top Related Articles:

  1. How to Find length of an Integer in Java
  2. Java StringBuffer setLength() Method
  3. Java Math.signum() Method
  4. Java Math.expm1() Method
  5. Java Math.min() Method

Tags: Java-Conversion

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.

– Chaitanya

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 – 2025 BeginnersBook . Privacy Policy . Sitemap