# Thread: Problem with rounding the integer type

1. Member Join Date
Dec 2013
Posts
56
Rep Power
0

## 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  Reply With Quote

2. ## 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  Reply With Quote

3. Senior Member Join Date
Feb 2014
Posts
219
Rep Power
7

## Re: Problem with rounding the integer type

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

4. Member Join Date
Dec 2013
Posts
56
Rep Power
0

## Re: Problem with rounding the integer type

I solved it by using (int)Math.rint().
Thank you to both of you anyway
Last edited by nat; 03-12-2014 at 09:17 PM.  Reply With Quote

5. ## Re: Problem with rounding the integer type

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.  Reply With Quote

java 