×

How to Convert double to int in Java

The double is a larger data type than int. When we assign a larger type value to a variable of smaller type, then we need to perform the explicit conversion. The conversion from double to int is called explicit conversion or narrowing conversion.

We know that the double data type can contain decimal digits. The decimal digits are truncated when we convert double value with decimal digits to int value.

Double value can contain decimal digits, so when we convert double value with decimal digits to int value, the decimal digits are truncated. There are two ways to convert double to int.

  • Using Typecasting
  • Using intValue() method

Using Typecasting

Typecasting is used to perform the conversion between two incompatible types. Typecast operator (data type) is used to perform typecasting in Java. The signature of typecasting is:

data_type variable_name = (target data type) variable to convert;

Example

In the following example, we have taken a variable pi of type double and initialized the value of pi 3.14159265359 to it. pi2 is a variable of type int which stores the converted value of pi. (int) is a typecast operator. pi is a variable which we want to convert into int type. The first println statement prints the pi value before truncation of decimal digits. The second println statement prints the pi value after truncation of decimal digits.

public class doubleTointExample
{ 
public static void main(String args[])
{ 
double pi=3.14159265359; 
int pi2=(int)pi; performing typecasting
System.out.println("Before truncation: " +pi); 
System.out.println("After truncation: " +pi2); 
}
}

Output

Before truncation: 3.14159265359
After truncation: 3

Using intValue() method

The intValue() is the built-in method of Integer wrapper class which belong to the java.lang package. It does not accept any parameter. It returns the value which is represented by the object after conversion to int type. Double wrapper class truncates all digits after the decimal point.

Example

In the following example, Double is a wrapper class, and d is an object of Double class. The new is a keyword that creates an object dynamically.  Double() automatically calls the constructor of Double class. The constructor parses a double value as an argument. We have taken another variable i of type int which stores the converted int value. The object d invokes the intValue() method of Integer class, and converts double type into int type. The first println statement prints the value before truncation. The second println statement prints the value after truncation.

public class doubleTointExample1
{ 
public static void main(String args[])
{ 
//Double d= 6782.432;           //use either this line or the line next to it both works same
Double d=new Double(6782.432);  
int i=d.intValue();  //using intValue() method
System.out.println("Before truncation: "+d); 
System.out.println("After truncation: "+i); 
}
}

Output

Before truncation: 6782.432
After truncation: 6782

Related Topics

Quick Sort in Java

Quick Sort in Java Like merge sort, quick sort also uses the divide and conquer approach to sort the given array or list. In quick sort, the sorting of an array...

6 minutes read.

Various operations on the Queue using Stack in Java

The Java Collections Framework's core data structures are the Stack and Queue. They are used to store and retrieve identical data in a presentation sequence. These two linear data structures...

7 minutes read.

Java SHA256

Definition: In cryptography, SHA is a hash function that takes 20 bytes of input and produces an approximate 40-digit hexadecimal integer as the hash result. Class for Message Digest: Java's MessageDigest Class,...

2 minutes read.

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 minutes read.

Model Class in Java

In this section, we will be acknowledged about the model class in Java, its purpose and its uses. Also, we will learn how is this created and leveraged in java. Model...

4 minutes read.

Java Comparable Interface

We implement comparable interface when we need a new logic for determining equality. if two objects are equal, the equals() method returns true and compareTo() method returns 0. The basic...

1 minute read.

Java Math pow() Method

The pow() method of Math class returns the value of first argument raised to the power of second argument i.e. ab. Syntax: public static double pow(double a, double b) Parameters: The parameters ‘a’ and...

3 minutes read.

Java Virtual Machine (JVM)

JVM is a virtual runtime environment to execute Java byte codes. The JVM doesn’t understand the keywords we used to write code. That is why it is converted into bytecode. It controls the...

3 minutes read.

Java Stringjoiner Class

StringJoiner is a class which is used to construct a sequence of characters which are separated by a delimiter. Optionally, it starts with a provided prefix and ended with the...

5 minutes read.

Java Integer max() method

The max() method of Integer class returns the greater of two int values. It returns the same result as by calling Math.max. Syntax public static int max(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Best Practices to use String Class in Java

Use String Builder or String Buffer for String concatenation in place of + operator.Compare two strings by equals( ) method instead == operator.Call .equals( ) method on a known String...

3 minutes read.

Compile time vs Runtime in java

Introduction: This article will discuss compile time vs. runtime in java. Compile time and runtime are two programming terms utilized in software improvement. Compile time is when the source code is...

3 minutes read.

Java File Input Stream

Java makes use of stream ideas to speed up input and output processes. All packages of input and output streams are contained in java.io.package. Stream: A stream is nothing more...

5 minutes read.

Java Output Formatting

Sometimes we want a program's output to be displayed in a specific format. The printf () function in the C programming language can be used to achieve this. We will...

4 minutes read.

Java Try Keyword

The try block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java try block has...

3 minutes read.

Array and String with Examples in Java

Array in Java: An array in Java is a group of variables with similar types that have a common name. The arrays used in Java differ from those used in C/C++. Key...

6 minutes read.

Producer Consumer Problem in Java Using Synchronized Block

The producer-consumer dilemma is a well-known instance of a multi-process synchronization issue in computing. Two processes—the producer and the consumer—are described in the issue, and they share a single, fixed-size...

4 minutes read.

Static vs Non-Static in Java

A static method can access and update the value of a static data member. The static keyword is mostly used in Java for memory management. The static keyword can be...

6 minutes read.

Java Math ceil() Method

The ceil() method of Math class returns the smallest double value which is closest to negative infinity and is greater than or equal to the argument. Syntax: public static double ceil(double a) Parameters: The...

2 minutes read.

Difference between String, StringBuffer and StringBuilder in java

What is a string in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.