Rather than an "8.75" returned.

Which method? Thanks.

Rather than an "8.75" returned.

Which method? Thanks.

+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.

Bozho
2010-07-08 07:07:33

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.

John P
2010-07-08 07:11:57

no, it will return 9 (if the arguments are `double`)

Bozho
2010-07-08 07:14:08
Yep, you are right. Me bad.

John P
2010-07-08 07:15:41
you can edit your answer to write the correct method.

Bozho
2010-07-08 07:19:21
well, I edited it for you

Bozho
2010-07-08 07:48:32
+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.

MartinStettner
2010-07-08 07:21:18

A:

Several very helpful answers here. Thank you.

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.

Jesper
2010-07-08 07:54:49
Most people will appreciate it just fine if I don't. This place is about mutual support.

2010-07-08 08:01:51
People won't get upset if you don't pick their answer. They will get upset if you don't pick any answer.

Noel M
2010-07-08 08:16:07
+1
A:

```
int x = 35/4
```

integer division causes 8 to be returned instead of 8.75

Saher
2010-07-08 07:48:38