Converting double to integer in Java

463,734

Solution 1

is there a possibility that casting a double created via Math.round() will still result in a truncated down number

No, round() will always round your double to the correct value, and then, it will be cast to an long which will truncate any decimal places. But after rounding, there will not be any fractional parts remaining.

Here are the docs from Math.round(double):

Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:

(long)Math.floor(a + 0.5d)

Solution 2

For the datatype Double to int, you can use the following:

Double double = 5.00;

int integer = double.intValue();

Solution 3

Double perValue = 96.57;
int roundVal= (int) Math.round(perValue);

Solved my purpose.

Share:
463,734
vdMandele
Author by

vdMandele

Updated on July 23, 2022

Comments

  • vdMandele
    vdMandele almost 2 years

    In Java, I want to convert a double to an integer, I know if you do this:

    double x = 1.5;
    int y = (int)x;
    

    you get y=1. If you do this:

    int y = (int)Math.round(x);
    

    You'll likely get 2. However, I am wondering: since double representations of integers sometimes look like 1.9999999998 or something, is there a possibility that casting a double created via Math.round() will still result in a truncated down number, rather than the rounded number we are looking for (i.e.: 1 instead of 2 in the code as represented) ?

    (and yes, I do mean it as such: Is there any value for x, where y will show a result that is a truncated rather than a rounded representation of x?)

    If so: Is there a better way to make a double into a rounded int without running the risk of truncation?


    Figured something: Math.round(x) returns a long, not a double. Hence: it is impossible for Math.round() to return a number looking like 3.9999998. Therefore, int(Math.round()) will never need to truncate anything and will always work.

  • qbert220
    qbert220 almost 13 years
    The important point is that the rounding is done within the round method. A long value is returned, which can safely be cast to an int (assuming returned value will always be within int range).
  • vdMandele
    vdMandele almost 13 years
    Yes. Can't imagine a /long/ to int giving problems. Obviously! Should've figured this :(
  • Nelda.techspiress
    Nelda.techspiress almost 7 years
    While it is true that truncation will not occur because of the rounding, you still may not get the expected results simply due to the casting from double which is a very large number [max double: 1.7976931348623157E308] to int which is much much smaller [max int: 2147483647]. Just something to keep in mind.
  • user207421
    user207421 over 6 years
    He is looking for a rounded value.
  • murkle
    murkle over 4 years
    I don't think that will give what you expect for 4.99999999999 (will give 4 not 5)
  • laur34
    laur34 over 3 years
    Mine says "double cannot be dereferenced".
  • Charlie
    Charlie almost 3 years
    This truncates (floors), and does not round. Use at your own risk.