| Author |
Rounding 'int' values to nearest Tens
|
Raj Menon
Ranch Hand
Joined: Oct 18, 2005
Posts: 45
|
|
Hi , Are there any built-in methods for rounding 'int' to nearest Tens? For example 72 should be rounded to 70 where as 75 or >75 should be rounded to 80. If there are no built-in methods,atleast give a suggestion.
|
 |
Purushoth Thambu
Ranch Hand
Joined: May 24, 2003
Posts: 425
|
|
|
Divide the number by 10 and cast it to double. Apply Math.round function and multiple by 10 and typecast back to int .
|
 |
Ankur Sharma
Ranch Hand
Joined: Dec 27, 2005
Posts: 1234
|
|
Originally posted by Rajes Kodali: Hi , Are there any built-in methods for rounding 'int' to nearest Tens? For example 72 should be rounded to 70 where as 75 or >75 should be rounded to 80. If there are no built-in methods,atleast give a suggestion.
Well there is no built-in function although but you can acheive this by simple logic. If you have the int variable then you can check out the following example. Hope this will helps you out.
|
The Best way to predict your future is to create it
Ankur Sharma
|
 |
Chetan Parekh
Ranch Hand
Joined: Sep 16, 2004
Posts: 3636
|
|
I have developed following logic,however I suggest you to leverage existing APIs of Java. [ September 26, 2006: Message edited by: Chetan Parekh ]
|
My blood is tested +ve for Java.
|
 |
Peter Chase
Ranch Hand
Joined: Oct 30, 2001
Posts: 1970
|
|
Oh my goodness, what a lot of complex solutions to a simple task! (Yes, I know, starting my post like this will doubtless mean my solution is wrong and I will be made to eat humble pie, but anyway...) There is no need to use floating-point or any Math methods. Stick to integer arithmetic, which is faster, simpler and less prone to errors. Just add 5, divide by 10, multiply by 10.
|
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
|
 |
Chetan Parekh
Ranch Hand
Joined: Sep 16, 2004
Posts: 3636
|
|
|
You are right Peter Chase that we should leverage the existing APIs of Java to achieve this. I was just free and develop above logic. I have added comment in my post.
|
 |
Peter Chase
Ranch Hand
Joined: Oct 30, 2001
Posts: 1970
|
|
Originally posted by Chetan Parekh: we should leverage the existing APIs of Java to achieve this
Strictly, one does not need to leverage any API at all for this. The pure language feature of integer arithmetic is sufficient, as demonstrated above.
|
 |
Ankur Sharma
Ranch Hand
Joined: Dec 27, 2005
Posts: 1234
|
|
Originally posted by Peter Chase: as demonstrated above.
Which one you are talking above...???
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
|
My guess would be Peter's own solution i.e. the one that just uses integer arithmetic.
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Raj Menon
Ranch Hand
Joined: Oct 18, 2005
Posts: 45
|
|
|
Thanks a lot to each and every one
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
|
I've used Peter's ((n+5)/10)*10 for decades from COBOL forward. I've even had situations where it was (n+9) to always round up, or (n+4)/5 to round up to nearest 5. Good clean fun.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Peter Chase
Ranch Hand
Joined: Oct 30, 2001
Posts: 1970
|
|
Originally posted by Stan James: I've used Peter's ((n+5)/10)*10 for decades from COBOL forward
Works nicely in FORTRAN, too
|
 |
 |
|
|
subject: Rounding 'int' values to nearest Tens
|
|
|