90

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

Joren
  • 3,068
  • 25
  • 44
badpanda
  • 2,446
  • 5
  • 34
  • 45
  • possible duplicate of [How to convert float to int with Java](http://stackoverflow.com/questions/1295424/how-to-convert-float-to-int-with-java) – Ciro Santilli OurBigBook.com Jan 26 '15 at 12:50
  • I believe that floats and doubles are different data types in Java. – badpanda Jan 27 '15 at 05:20
  • I think they're too close because both are primitives and floating point types. Answers are the same and are very likely to continue being so. – Ciro Santilli OurBigBook.com Jan 27 '15 at 07:02
  • 1
    Actually, the floating point question is more accurately asking how to round a float (as per some unspecified rounding standard). This question, which could probably be renamed more appropriately to 'How to round a double towards zero and cast to int in Java?' is similar to asking how to floor a double, except that flooring rounds to negative infinity rather than towards zero. The answers are quite different although the titles of the questions are similar. – badpanda Feb 02 '15 at 00:04

9 Answers9

148

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)

DGinzberg
  • 146
  • 9
Xorlev
  • 8,561
  • 3
  • 34
  • 36
  • 28
    Note that `Math.floor` *does* produce a different result for negative numbers than a simple typecast. – Joey Jan 26 '10 at 23:40
  • 4
    It depends on what the OP means by "round down". The cast rounds it "down" toward zero, while Math.floor rounds towards negative infinity. – Lambda Fairy Dec 15 '11 at 03:24
  • 5
    Also note that an int cannot represent all the possible integer values that a double can. If you wish to truncate a double-precision value, then cast to a 64-bit integer value like long. `System.out.println((long)(9.9999e300));` – Monroe Thomas Jul 02 '12 at 18:09
  • 2
    Although IEEE 754 [rounding rules](https://en.wikipedia.org/wiki/IEEE_floating_point#Rounding_rules) uses the term *truncation* for round-towards-zero, and uses the synonymous terms *round down* and *floor* for round-towards-negative-infinity, Java's [`BigDecimal`](https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html#field.summary) and [`RoundingMode`](https://docs.oracle.com/javase/8/docs/api/java/math/RoundingMode.html#enum.constant.summary) classes uses `ROUND_DOWN` for round-towards-zero and `ROUND_FLOOR` for round-towards-negative-infinity. – Andreas Nov 11 '16 at 23:18
33

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.

JavaDrip
  • 331
  • 3
  • 3
  • 6
    You're completely right, it doesn't. I saw that this thread pops up when Google-ing for "Java int typecast double," and figured that someone looking for a way to round up/down directly with the (int) typecast could use this. This isn't posted using a stackoverflow account (so I'm not trying to boost my reputation, if that's taken seriously here), just trying to help spread a cool trick I use quite often. :) Cheers! – JavaDrip Jul 02 '12 at 18:23
  • 4
    Nice but remember this works only with **positive** doubles. For instance, if you round -0.8 with this trick, then the code `System.out.println(""+ (int)(-0.8d + 0.5))` prints 0, instead of -1 as expected – Gil Vegliach Jan 23 '14 at 19:53
19

(int)99.99999

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

Sergey Nemchinov
  • 1,348
  • 15
  • 21
leeeroy
  • 11,216
  • 17
  • 52
  • 54
13
Math.floor(n)

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

Austin Fitzpatrick
  • 7,243
  • 3
  • 25
  • 22
5

This works fine int i = (int) dbl;

fastcodejava
  • 39,895
  • 28
  • 133
  • 186
5

new Double(99.9999).intValue()

donfuxx
  • 11,277
  • 6
  • 44
  • 76
Nico.S
  • 753
  • 8
  • 12
1

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.

Amitsharma
  • 1,577
  • 1
  • 17
  • 29
0

Try using Math.floor.

John
  • 15,990
  • 10
  • 70
  • 110
0

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.

Adil B
  • 14,635
  • 11
  • 60
  • 78
R.Nish
  • 73
  • 2
  • 15