Problem with rounding the integer type

Hi,

I have this code line:

String sectionText = String.valueOf((int)percents.getYellowPercent());

and I want the integer value to be displayed, instead of double. This works, but when the program rounds the double into integer, if the number is for example 75,56 (this is %) or 3,5 I would like it to be rounded in 76 (or 3,6 for the other example) since as it is at this moment, when all the calculated integers are summed, the result is not 100%, but 99%.

Can anybody help me with this issue?

Thanks

Re: Problem with rounding the integer type

One way would be to add 0.5 to the double before casting it to int. (int)(1.2+.5) = 1 and (int)(1.5+.5) = 2

Re: Problem with rounding the integer type

See also the Math class at Math (Java Platform SE 7 ). It has specific methods for rounding.

Re: Problem with rounding the integer type

I solved it by using (int)Math.rint().

Thank you to both of you anyway

Re: Problem with rounding the integer type

Quote:

How can I implement your suggestion

You don't need to know what the value is before adding and casting.

Try some small,sample programs to see what adding 0.5 and casting to int will do.

If there is a problem, show the input and the desired output.