Help coderanch get a
new server
by contributing to the fundraiser
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
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Rounding up double value

Greenhorn
Posts: 12
• Number of slices to send:
Optional 'thank-you' note:
Hie,
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("")) {
val=df.format(val);
}
} catch (Exception e) {
System.out.println(String.valueOf(e));
}
return val;
**********************************************
Anyone have any idea?
Thanks~!!

Ranch Hand
Posts: 1970
1
• Number of slices to send:
Optional 'thank-you' note:
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.

Marshal
Posts: 79564
380
• Number of slices to send:
Optional 'thank-you' note:
Another way to do it:-
Multiply by 100, add 0.5, use the Math.round() method, and multiply by 0.01.

CR

Ranch Hand
Posts: 689
• Number of slices to send:
Optional 'thank-you' note:
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..

Ranch Hand
Posts: 135
• Number of slices to send:
Optional 'thank-you' note:
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.

Marshal
Posts: 28271
95
• Number of slices to send:
Optional 'thank-you' note:
The BigDecimal class has a setScale() method that can be given one of several rounding modes, including "ROUND_UP".

Greenhorn
Posts: 15
• Number of slices to send:
Optional 'thank-you' note:
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?

Kentaro

 Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad: We need your help - Coderanch server fundraiser https://coderanch.com/t/782867/Coderanch-server-fundraiser