Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Round off a number

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have data coming in like 42.44, 28.69, 28.89, 22.22, etc., I need to round these numbers up for 0.5, like the result of rounding off the above numbers should be 42.50, 29.0, 29.0, 22.50. How do I do it?
The above numbers are of type double.
Thanks in advance
Sreeni.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the documentation for java.lang.Math (you should try the round() and floor() methods).
Come back if you are still having problems
 
sreeni san
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using round() or floor() will not help me as round() would round off 42.44 to 42 and floor() will also round 42.44 to 42. In case the number 28.69, the round() would round off to 29 and floor() to 28.
This is not I want, I want the numbers to get rounded up to next 0.50 as mentioned in the examples in my question.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:
 
rom chatterjee
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My apologies.
This tech tip from Sun seems to cover what you are trying to do. I shall check back to if this still isn't what you need.
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where did you find a round method that takes 2 parameters?
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Elouise Kivineva:
Where did you find a round method that takes 2 parameters?


If you are referring to:

I wrote it - look a little farther down in the code.
reply
    Bookmark Topic Watch Topic
  • New Topic