Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community that shares solutions with each other.

If you think you have something to add here.

you are welcome with your fresh ideas - List Articles

Java - how to cast Double object to primitive int?

0 contributions
11 points

1. Problem description

How to cast Double object to primitive int type?

Double num = 3.14; // original - Double object
int num = 3;       // expected - primitive int

2. Solution with Double.intValue()

We can use Double.intValue() method as it returns primitive int.

Code example: 

Double num = 3.14;
int intValue = num.intValue();
System.out.println(intValue); // 3

Output:

3

3. Solution with casting

  1. Cast Double to primitive double
  2. Then cast primitive double to primitive int

Code example:

Double num = 3.14;
int intValue = (int) (double) (num);
System.out.println(intValue); // 3

Output:

3

Merged questions

  1. Java - cast big Double to small integer
  2. Java - convert Double object to primitive int
  3. How to convert big Double to primitive int type in java?
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more