• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Rounding up a float value

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone help me in rounding up numbers,
it should be like if its
1.5-->2,
1.6-->2,
1.9-->2 but if its 1.3 or 1.2 or 1.4 it should result 1.

i tried using math.ceil(135/100.0); it results 2 but I need 1 for 1.3 and 2 for 1.5 and more.. please help
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Akshata Alandker,

You can use

Math.round(i) i can be any float or double value.
 
lowercase baba
Posts: 13073
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what "math.ceil" is...did you write that yourself? Or did you mean "Math.ceil"?

If the former, I don't know what to tell you. If the latter, then reading the API would tell you:

Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.

perusing the rest of the Math API would let you find the round() method.
 
Akshata Alandker
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks santhosh varala kumar and fred rosenberger, that was of great help..
 
Marshal
Posts: 76450
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Beware of round(). Read its documentation carefully to find what it actually does.
You might want to display the numbers rounded rather than rounding their values. In which case I suggest you look at the % tags, which you can find in the Java Tutorials if you search with ctrl‑F‑formatting. Also look in the Formatter class, but the link covers about 30 pages!
 
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic