How to Cast a Double to an Int in Java by Rounding It Down

How to cast a double to an int in Java by rounding it down?

Casting to an int implicitly drops any decimal. No need to call Math.floor() (assuming positive numbers)

Simply typecast with (int), e.g.:

System.out.println((int)(99.9999)); // Prints 99

This being said, it does have a different behavior from Math.floor which rounds towards negative infinity (@Chris Wong)

Convert double to Int, rounded down

If you explicitly cast double to int, the decimal part will be truncated. For example:

int x = (int) 4.97542;   //gives 4 only
int x = (int) 4.23544; //gives 4 only

Moreover, you may also use Math.floor() method to round values in case you want double value in return.

Rounding a double to turn it into an int (java)

What is the return type of the round() method in the snippet?

If this is the Math.round() method, it returns a Long when the input param is Double.

So, you will have to cast the return value:

int a = (int) Math.round(doubleVar);

How the explicit cast from double to int rounds in Java?

The double literal 99.9999999999999 can be represented as a double which is less than 100, so the cast to int truncates the decimal part and 99 is the result.

The double literal 99.999999999999999999999999999999999999999 has the closest actual value of 100, and casting to int is simply 100 here.

System.out.println(99.9999999999999);
System.out.println((int)(99.9999999999999));
System.out.println(99.9999999999999 == 100.0);

System.out.println(99.999999999999999999999999999999999999999);
System.out.println((int)(99.999999999999999999999999999999999999999));
System.out.println(99.999999999999999999999999999999999999999 == 100.0);

I used 100.0 as a double literal here, to compare 2 double values to see if they are the exact same double value.

This prints out:

99.9999999999999
99
false
100.0
100
true

The JLS, Section 3.10.2 covers floating point literals:

The details of proper input conversion from a Unicode string representation of a floating-point number to the internal IEEE 754 binary floating-point representation are described for the methods valueOf of class Float and class Double of the package java.lang.

And Double.valueOf javadocs state:

Otherwise, s is regarded as representing an exact decimal value in the usual "computerized scientific notation" or as an exact hexadecimal value; this exact numerical value is then conceptually converted to an "infinitely precise" binary value that is then rounded to type double by the usual round-to-nearest rule of IEEE 754 floating-point arithmetic, which includes preserving the sign of a zero value.

(emphasis mine)

That is, the value is rounded to produce the actual double value.

Java Standard On Result Of Casting A Double To An Int

Yes, it will drop the decimal point. That means it will round towards zero, so it will round down if it is positive, but it will round up if it is negative.

See §5.1.3 in the JLS:

Otherwise, if the floating-point number is not an infinity, the floating-point value is rounded to an integer value V, rounding toward zero using IEEE 754 round-toward-zero mode (§4.2.3).

Turning a double to an int in Java

Java does widening conversions without help (e.g. int to double) but for a narrowing conversion you need an explicit cast.



Related Topics



Leave a reply



Submit