aspose file tools*
The moose likes Java in General and the fly likes Rounding up double value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Rounding up double value" Watch "Rounding up double value" New topic

Rounding up double value

Yong Ming Wai

Joined: Aug 31, 2004
Posts: 12
I need to to round double value to a 2 decimal point value.However,
the value is to be round up instead round down as per default.
E.g : 2.142 -> 2.15 (default is 2.14)
or 2.1425 -> 2.15 (default is 2.14)
Code shown below return a round down value.
try {
DecimalFormat df = new DecimalFormat("RM#,###,###,###.00");
if (!val.equals(null) || !val.equals("")) {
} catch (Exception e) {
return val;
Anyone have any idea?

Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Just add 0.99999... before rounding. The exact number of 9s could be a little tricky to determine; it should be perhaps a couple fewer than the typical number of significant decimal digits in a double.

If you have negative values to deal with, think about whether this will do what you want, for those.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 39409
Another way to do it:-
Multiply by 100, add 0.5, use the Math.round() method, and multiply by 0.01.

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
i have one idea but i dont know whether you will accept or not
just conver the number into String and then modify the number using substring method..

SCJP 1.4
Adam Richards
Ranch Hand

Joined: Nov 03, 2005
Posts: 135
Do you actually want to round the binary value, or just the string representation? Strings are easier, since there are so many nice formatters. Might even be safer, depending on your app, since it doesn't modify the original value.
Paul Clapham

Joined: Oct 14, 2005
Posts: 18657

The BigDecimal class has a setScale() method that can be given one of several rounding modes, including "ROUND_UP".
Kentaro Shinbashi

Joined: Jun 08, 2006
Posts: 15
Yong Ming Wai:

I think the above suggestions are good. But I think it is also important to know what your application is.

You want to round to the second decimal place, which suggests that you might be dealing with monetary values, like dollars:

$40.00 / 3 = $13.3333333...
Rounded up is $13.34

So, if this is what you are dealing with, then the realm of questions changes a bit. Often, when dealing with financial calculations, it is better in the longrun to use all integer representations, for example, using an int to represent cents, and simply divide by 100 to get dollars.

Or else, use BigDecimal class.

The reason for this is that over time, errors accumulate, and sometimes, if adding a very small number to a very large number, information gets lost.

So, forgive me for going one step beyond your specific question, but I thought it might be relevent. Is it what you are trying to do?

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Rounding up double value