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

JavaCasting

Java Problem Overview


I need to cast a double to an int in Java, but the numerical value must always round down. i.e. 99.99999999 -> 99

Java Solutions


Solution 1 - Java

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)

Solution 2 - Java

To cast a double to an int and have it be rounded to the nearest integer (i.e. unlike the typical (int)(1.8) and (int)(1.2), which will both "round down" towards 0 and return 1), simply add 0.5 to the double that you will typecast to an int.

For example, if we have

double a = 1.2;
double b = 1.8;

Then the following typecasting expressions for x and y and will return the rounded-down values (x = 1 and y = 1):

int x = (int)(a);   // This equals (int)(1.2) --> 1
int y = (int)(b);   // This equals (int)(1.8) --> 1

But by adding 0.5 to each, we will obtain the rounded-to-closest-integer result that we may desire in some cases (x = 1 and y = 2):

int x = (int)(a + 0.5);   // This equals (int)(1.8) --> 1
int y = (int)(b + 0.5);   // This equals (int)(2.3) --> 2

As a small note, this method also allows you to control the threshold at which the double is rounded up or down upon (int) typecasting.

(int)(a + 0.8);

to typecast. This will only round up to (int)a + 1 whenever the decimal values are greater than or equal to 0.2. That is, by adding 0.8 to the double immediately before typecasting, 10.15 and 10.03 will be rounded down to 10 upon (int) typecasting, but 10.23 and 10.7 will be rounded up to 11.

Solution 3 - Java

(int)99.99999

It will be 99. Casting a double to an int does not round, it'll discard the fraction part.

Solution 4 - Java

Math.floor(n)

where n is a double. This'll actually return a double, it seems, so make sure that you typecast it after.

Solution 5 - Java

This works fine int i = (int) dbl;

Solution 6 - Java

new Double(99.9999).intValue()

Solution 7 - Java

try with this, This is simple

double x= 20.22889909008;
int a = (int) x;

this will return a=20

or try with this:-

Double x = 20.22889909008;
Integer a = x.intValue();

this will return a=20

or try with this:-

double x= 20.22889909008;
System.out.println("===="+(int)x);

this will return ===20

may be these code will help you.

Solution 8 - Java

Try using Math.floor.

Solution 9 - Java

In this question:

1.Casting double to integer is very easy task.

2.But it's not rounding double value to the nearest decimal. Therefore casting can be done like this:

double d=99.99999999;
int i=(int)d;
System.out.println(i);

and it will print 99, but rounding hasn't been done.

Thus for rounding we can use,

double d=99.99999999;
System.out.println( Math.round(d));

This will print the output of 100.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbadpandaView Question on Stackoverflow
Solution 1 - JavaXorlevView Answer on Stackoverflow
Solution 2 - JavaJavaDripView Answer on Stackoverflow
Solution 3 - JavaleeeroyView Answer on Stackoverflow
Solution 4 - JavaAustin FitzpatrickView Answer on Stackoverflow
Solution 5 - JavafastcodejavaView Answer on Stackoverflow
Solution 6 - JavaNico.SView Answer on Stackoverflow
Solution 7 - JavaAmitsharmaView Answer on Stackoverflow
Solution 8 - JavaJohnView Answer on Stackoverflow
Solution 9 - JavaR.NishView Answer on Stackoverflow