×

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

Fork Join in Java

Multithreaded processors are being introduced in new computer systems today. The operation is faster due to multicore CPUs. Therefore, it becomes essential for a programmer to leverage multithreaded processors effectively...

4 minutes read.

Java program to determine whether all leaves are at same level

In this program, we must determine whether or not all of the binary tree's leaves are at the same level. If a node has no child nodes, it is said to...

3 minutes read.

Java Math tan() Method

The tan() method of Java Math class returns the trigonometric tangent of the specified angle. Syntax: public static double tan(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The tan() method...

2 minutes read.

String isnullorempty in Java

What do you mean by String? 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.

Java Font

The font is a Java class that is a part of java.awt package. The Serializable interface is implemented by it. The direct recognized child of a Java Font class is...

8 minutes read.

Java Math nextDown() Method

The nextDown() method of Math class returns the floating-point number adjacent to the argument in direction of the negative infinity. Syntax: public static double nextDown (double d)public static float nextDown (float f) Parameters: The...

2 minutes read.

Java New Keyword

To create a class instance in Java, use the new keyword. In other words, it returns a reference to the memory that was allocated for a new object and instantiates...

3 minutes read.

Java Read File to String

There are different ways to deal with forming and examining a text record. This is normal while dealing with various applications. There are different ways to deal with looking at a...

6 minutes read.

Java Maven Silicon

Maven is a build automation tool that is mostly used for Java applications. It allows you to manage the build, reporting, and documentation of a project from the central location. Maven provides...

4 minutes read.

Empty Statement in Java

The three sorts of statements in Java are control, expression, and declaration statements. Additionally, another statement is referred to as an empty statement. In this section, we will discuss about...

4 minutes read.

Zygodromes in Java

Zygodrome is a positive number created by the same digits running non-trivially. A number is called a zygodrome if identical digits constantly occur together (in pairs). The Greek word "zyg"...

4 minutes read.

String Pool in Java

String Pool in Java: String is one of the most important discussed topics in Java. There are a lot of concepts related to the String and one of them is...

5 minutes read.

Java Integer class

The Integer class wraps a primitive int type value in an object. Its object contains only a single field whose type is int. Methods: The java.lang.Integer class provides several different methods for...

4 minutes read.

How to Convert long to int in Java

How to Convert long to int in Java When we assign a larger type value to a variable of smaller type, then we need to perform explicit casting for the conversion....

2 minutes read.

Alice and Bob Problem Java

Alice and Bob were two friends who liked to play games. Both together found a game, which has the description below. The game begins with an integer num, used to create...

2 minutes read.

Java Interface Keyword

An interface is also known as the blueprint in Java. It has constants of static values and methods of abstraction. The interface is a mechanism used by Java to declare...

3 minutes read.

How to Set Java_home in Linux

To set the Java_home in Linux, we must follow several steps to make us understand it easily.  Java follows the principle called WORA (write once run anywhere). We know that java...

3 minutes read.

Swapping Program in Java

Swapping Program in Java The swapping program in Java is used to interchange the values of the two variables. For example, if X = 12 and Y = 24, then the...

4 minutes read.

Java Primitive Data Types

Primitive data types are the simplest data types in a programming language. They’re predefined in the language. The names of the primitive types are quite descriptive of the values that...

2 minutes read.

How to open the Java control panel

The many methods for launching the Java control panel will be covered in this section. We will also go through how the Java Control Panel can be used. Java Control Panel The...

2 minutes read.