| Author |
howto round integer ?
|
Nakata kokuyo
Ranch Hand
Joined: Apr 13, 2005
Posts: 437
|
|
hi, good day, if i have a integer number = "1234567" , and i would like to round the last 2 digit , means if last 2 digit more than 50, it will become "1234600" if we hv integer number "1234535" , it will round to 1234500, cause the last 2 digit is less than 50 , in my case, how to i round that ? is API have class to cater these numbering ? thank you for guidance
|
 |
vignesh hariharan
Ranch Hand
Joined: Jun 23, 2005
Posts: 77
|
|
i am not sure if your question is correct. i think it is 12345.67 which will be rounded to 12346 and 12345.35 will be rounded to 12345 correct me if i am wrong.
|
Regards,
vignesh
|
 |
Roy Simon
Ranch Hand
Joined: Aug 26, 2005
Posts: 62
|
|
hi, You want to round off integers?? i dont think the java Api offers anything of that sort and moreover its very simple.. 1.Create a custom class with an Integer Object.... 2.Create objects of the custom class by passing integers into the constructor... 3. Inside the constructor u can use the %50 operator and additional code to either add or truncate the tail... Now u get the required functionality U can use the intValue() method of the Integer class to get an int from the Integer!! Hope this helps... Regards Simon  [ April 01, 2006: Message edited by: Roy Simon ]
|
 |
Paul Santa Maria
Ranch Hand
Joined: Feb 24, 2004
Posts: 236
|
|
I'd suggest: i = (int)((num / 100.0) + 0.5) * 100;
|
Paul M. Santa Maria, SCJP
|
 |
Nakata kokuyo
Ranch Hand
Joined: Apr 13, 2005
Posts: 437
|
|
Paul Santa Maria , it look like good solution and simpler , at the moment, i just try on this as roy mentioned , here is my sample code
|
 |
 |
|
|
subject: howto round integer ?
|
|
|