301

7
Q:

## Given 35 / 4, I want an "8" returned.

Rather than an "8.75" returned.

Which method? Thanks.

+4  A:

cast to int

``````(int)
``````

example :

http://www.javaprogrammingforums.com/java-code-snippets-tutorials/314-how-type-cast-convert-double-integer.html

+10  A:

Use

``````Math.floor(..)
``````

Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.

Or, if you need to manipulate ints, not doubles, then simply assign to int:

``````int a = 35 / 4;
``````

Actually, if you work with the numbers directly, or with int variables, you will get an `int` result automatically.

+2  A:

explicitly cast the expression as int

A:

using

``````int myInt = (int) Math.floor(value);
``````

8.75 will be rounded to 8. Casting to int removes decimals in case value is a double.

no, it will return 9 (if the arguments are `double`)
Yep, you are right. Me bad.
well, I edited it for you
+12  A:

If I'm not completely wrong, `35/4` always evaluates to 8. Even in

``````double result = 35 / 4;
``````

you will get this result (as double, i.e. `8.0`). This is because `35` and `4` are both integers and therefore integer division is applied. The expression is first evaluated as integer and only the result is cast to a double value.

On the other hand, if you want the correct result `8.75` you' ll have to write

`````` double result = 35. / 4;
``````

in order to force floating point division to be applied. This is a common pitfall by the way.

Of course, if you have double values, like in

``````double a = 35;
double b = 4;
``````

you need to either explicitely cast to an `int` or use `Math.floor()` as suggested in previous posts.

So if I just write int/int, it's cool? I'll get an 8?
@user225626 Yes. Why don't you just try it out yourself?
I did before I accepted. I just wanted to make sure that's the language design.
A:

That said, I'd accept more than one answer--or none, because that's the way SO is set up. So thanks all. Much appreciated. I'll up your answers, but unfortunately no green check (much as I'd like). I'd be playing favorites.

Thanks again all for your help. (If you want to recommend among yourselves which I should 'accept', I'd be happy to consider.)

UPDATED: Okay, I accepted one.

Just select the answer that's most useful to you, or maybe the one that answered your question first. People will not appreciate it if you don't pick one of the answers.
Most people will appreciate it just fine if I don't. This place is about mutual support.
I mean, all of these answers were useful to me.
People won't get upset if you don't pick their answer. They will get upset if you don't pick any answer.
I've gotten that in the past from a few users. I don't care. But, also, I've seen more than one case where I would have checked off something and luckily I didn't because in reconsideration something else was better for me. As it happens, this page included, in fact.
I have a new question posted under MartinStettner.
+1  A:
``````int x = 35/4
``````

integer division causes 8 to be returned instead of 8.75