• Post Reply Bookmark Topic Watch Topic
  • New Topic

Rounding 'int' values to nearest Tens  RSS feed

 
Raj Menon
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Divide the number by 10 and cast it to double. Apply Math.round function and multiple by 10 and typecast back to int .
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have developed following logic,however I suggest you to leverage existing APIs of Java.


[ September 26, 2006: Message edited by: Chetan Parekh ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:


as demonstrated above.


Which one you are talking above...???
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess would be Peter's own solution i.e. the one that just uses integer arithmetic.
 
Raj Menon
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot to each and every one
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
I've used Peter's ((n+5)/10)*10 for decades from COBOL forward


Works nicely in FORTRAN, too
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!