This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Rounding up a float value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Rounding up a float value" Watch "Rounding up a float value" New topic

Rounding up a float value

Akshata Alandker

Joined: Sep 11, 2012
Posts: 19
Can anyone help me in rounding up numbers,
it should be like if its
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
santhosh varala kumar

Joined: Nov 05, 2009
Posts: 25
Dear Akshata Alandker,

You can use

Math.round(i) i can be any float or double value.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Akshata Alandker

Joined: Sep 11, 2012
Posts: 19
Thanks santhosh varala kumar and fred rosenberger, that was of great help..
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46412
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!
I agree. Here's the link:
subject: Rounding up a float value
It's not a secret anymore!