Rounding Bigdecimal values with 2 Decimal Places
I think that the RoundingMode
you are looking for is ROUND_HALF_EVEN
. From the javadoc:
Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor. Behaves as for ROUND_HALF_UP if the digit to the left of the discarded fraction is odd; behaves as for ROUND_HALF_DOWN if it's even. Note that this is the rounding mode that minimizes cumulative error when applied repeatedly over a sequence of calculations.
Here is a quick test case:
BigDecimal a = new BigDecimal("10.12345");
BigDecimal b = new BigDecimal("10.12556");
a = a.setScale(2, BigDecimal.ROUND_HALF_EVEN);
b = b.setScale(2, BigDecimal.ROUND_HALF_EVEN);
System.out.println(a);
System.out.println(b);
Correctly prints:
10.12
10.13
UPDATE:
setScale(int, int)
has not been recommended since Java 1.5, when enums were first introduced, and was finally deprecated in Java 9. You should now use setScale(int, RoundingMode)
e.g:
setScale(2, RoundingMode.HALF_EVEN)
Rounding BigDecimal to *always* have two decimal places
value = value.setScale(2, RoundingMode.CEILING)
round up to 2 decimal places in java?
Well this one works...
double roundOff = Math.round(a * 100.0) / 100.0;
Output is
123.14
Or as @Rufein said
double roundOff = (double) Math.round(a * 100) / 100;
this will do it for you as well.
BigDecimal with at least two decimals
Actually, you still can use setScale
, you just have to check if the current scale if greater than 2 or not:
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("3.001");
bd = bd.setScale(Math.max(2, bd.scale()));
System.out.println(bd);
}
With this code, a BigDecimal
that has a scale lower than 2 (like "3"
) will have its scale set to 2, and if it's not, it will keep its current scale.
Discrepancy in decimal rounding results
According to the JavaDoc of RoundingMode.HALF_EVEN
:
Rounding mode to round towards the {@literal "nearest neighbor"} unless both neighbors are equidistant, in which case, round towards the even neighbor.
That means, with a scale of 0
that you set in the .setScale()
(meaning you want 0 decimals):
3084.5
is equidistant from3084
and3085
, so it will be the even neighbor (hence3084
that is even, not3085
that is odd).3084.51
is not equidistant, it is0.01
closer to3085
than3084
, hence it will be the nearest neighbor.
Java BigDecimal Rounding while having pointless 0's in the number
Providing you have already performed your rounding, you can simply use DecimalFormat("0.#")
.
final double value = 5.1000;
DecimalFormat format = new DecimalFormat("0.#");
System.out.println(format.format(value));
The result here will be 5.1
without the trailing zeroes.
Related Topics
How to Convert a String to Another Locale
Where Is the Documentation for the Values() Method of Enum
Converting Secret Key into a String and Vice Versa
Named Placeholders in String Formatting
<Form Action="/Sampleservlet" Giving Me Exception
How to Change the Dock Icon of a Java Program
Create a New Line in Java's Filewriter
How to Install the Jdk on Ubuntu Linux
Static Allocation in Java - Heap, Stack and Permanent Generation
Is Java a Compiled or an Interpreted Programming Language
Using Jasperreports with a Relative Path
Consistency of Hashcode() on a Java String
Check If a String Contains a Special Character
Java Date - Insert into Database
How to Add Unique Jcomboboxes to a Column in a Jtable (Java)