Results 1 to 5 of 5
 03072014, 11:25 PM #1Member
 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
 03072014, 11:33 PM #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
If you don't understand my response, don't ignore it, ask a question.
 03082014, 05:37 PM #3Senior 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.
 03122014, 08:36 PM #4Member
 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 anywayLast edited by nat; 03122014 at 09:17 PM.
 03122014, 08:43 PM #5
Re: Problem with rounding the integer type
How can I implement your suggestion
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.If you don't understand my response, don't ignore it, ask a question.
Similar Threads

is it possible to convert String type of value into integer using type conversion?
By chandrasekhar.melam in forum New To JavaReplies: 6Last Post: 09142012, 12:33 PM 
Double keeps rounding to integer?
By LucienMontierre in forum New To JavaReplies: 2Last Post: 06292012, 05:12 AM 
Problem with rounding up in a school project
By OlivierSillegue in forum New To JavaReplies: 10Last Post: 02132012, 03:10 AM 
Change the integer variable to type double  is this right?
By Keke101 in forum New To JavaReplies: 1Last Post: 01022012, 11:16 PM 
What is the difference between Integer class and int primitive data type?
By rajkobie in forum New To JavaReplies: 1Last Post: 04192011, 04:32 PM
Bookmarks