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
Using File.Listfiles with Filenameextensionfilter
Java Equivalent to PHP's Hmac-Sha1
How Does Facebook Add Badge Numbers on App Icon in Android
Asynctask.Executeonexecutor() Before API Level 11
Opencv Template Matching Example in Android
How to Connect Android with MySQL Using MySQL Jdbc Driver
Is It Ok to Save a JSON Array in Sharedpreferences
When Is Onattach Called During the Fragment Lifecycle
Why Doesn't Java Throw an Exception When Dividing by 0.0
Why Hypot() Function Is So Slow
Rxjava Android How to Use the Zip Operator
Android Studio Error: "Manifest Merger Failed: Apps Targeting Android 12"
How to Send Data from Dialogfragment to a Fragment
How to Get Current Location in Googlemap Using Fusedlocationproviderclient
How to Properly Use Backwards Compatible Vector Drawable with the Latest Android Support Library