Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Round result to the nearest Integer

Greenhorn
Posts: 2
Hello all!

I'm trying to figure out how to round my division results to the nearest integer. I thought it would be fairly straight forward but obviously I'm not doing something right.

I wrote this extremely basic Android app for my wife and the formula requires that if there are any remainders or fractions, that the result needs to round to the nearest integer, so when it's multiplied by another variable, the final result will be correct. Instead, it always rounds down, which I'm certain is because I was initially using the INT type. But when I try to use DOUBLE for the variable, the remaining calculations that also rely on that variable give me an error: Incompatible Types, required INT, found: DOUBLE.

Can you not calculate mixed number types, such as integers with double or floating types?

Here is my calculation part of my code if anyone can shed some insight:

Master Rancher
Posts: 2310
76
hi Mike,

a standard trick is to add 0.5 to the value-to-be-rounded, and then cast it to an int.

Bartender
Posts: 585
9
You have to define what you mean by "round". There are usually considered to be three possible roundings: toward zero, toward minus infinity, and toward positive infinity.
Also, what do you want 0.5 to round to - 0.0 or 1.0 ? Some implementations will round odd halves up and even halves down, or vice-versa, or always down, or always up. Which you use depends on you needs - odd/even rounding is used to try and achieve a zero net bias in the long run.

Bartender
Posts: 10575
66

Mike Tompson wrote:I wrote this extremely basic Android app for my wife and the formula requires that if there are any remainders or fractions, that the result needs to round to the nearest integer, so when it's multiplied by another variable, the final result will be correct. Instead, it always rounds down, which I'm certain is because I was initially using the INT type. But when I try to use DOUBLE for the variable, the remaining calculations that also rely on that variable give me an error: Incompatible Types, required INT, found: DOUBLE.
Can you not calculate mixed number types, such as integers with double or floating types?

Well, you can't assign a double to an int without casting it, if that's what you mean; and since calculatedValue is a double, any expression that includes it will be a double.

Here is my calculation part of my code if anyone can shed some insight:

Here you go. I've basicially copied your code and made a few adjustments. Explanations below:1. The extra method is just to save typing, but you might find a use for it elsewhere.
2. Don't declare variables and then initialise. Do both at once.
3. calculatedValueC1 and calculatedValueC2 are part of calculatedValue, so why write out the expressions twice?
4. As I explained above the last two expression have to be cast to int before you can assign them.
And one general point:
5. Your variable names are either excessively long or short, and really don't mean anything to anyone reading your program.

HIH

Winston

Marshal
Posts: 57482
175
Welcome to the Ranch

I presume you have changed the arithmetic so you are no longer using integer arithmetic; that always uses round towards zero. I don't like your variable names, I am afraid; I cannot work out what c1 or c2 means.

 It is sorta covered in the JavaRanch Style Guide.