tags:

views:

301

answers:

7

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
+2  A: 

explicitly cast the expression as int

moonpie187
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
no, it will return 9 (if the arguments are `double`)
Bozho
Yep, you are right. Me bad.
John P
you can edit your answer to write the correct method.
Bozho
well, I edited it for you
Bozho
+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
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?
Jesper
I did before I accepted. I just wanted to make sure that's the language design.
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
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.
Noel M
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

Saher