File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes howto round integer ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "howto round integer ?" Watch "howto round integer ?" New topic

howto round integer ?

Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 465
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.

Roy Simon
Ranch Hand

Joined: Aug 26, 2005
Posts: 62
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...

[ 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: 465
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

I agree. Here's the link:
subject: howto round integer ?
It's not a secret anymore!