How to round a number to n decimal places in Java
Use setRoundingMode
, set the RoundingMode
explicitly to handle your issue with the half-even round, then use the format pattern for your required output.
Example:
DecimalFormat df = new DecimalFormat("#.####");
df.setRoundingMode(RoundingMode.CEILING);
for (Number n : Arrays.asList(12, 123.12345, 0.23, 0.1, 2341234.212431324)) {
Double d = n.doubleValue();
System.out.println(df.format(d));
}
gives the output:
12
123.1235
0.23
0.1
2341234.2125
EDIT: The original answer does not address the accuracy of the double values. That is fine if you don't care much whether it rounds up or down. But if you want accurate rounding, then you need to take the expected accuracy of the values into account. Floating point values have a binary representation internally. That means that a value like 2.7735 does not actually have that exact value internally. It can be slightly larger or slightly smaller. If the internal value is slightly smaller, then it will not round up to 2.7740. To remedy that situation, you need to be aware of the accuracy of the values that you are working with, and add or subtract that value before rounding. For example, when you know that your values are accurate up to 6 digits, then to round half-way values up, add that accuracy to the value:
Double d = n.doubleValue() + 1e-6;
To round down, subtract the accuracy.
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.
How to round a number to n decimal places in Java
Use setRoundingMode
, set the RoundingMode
explicitly to handle your issue with the half-even round, then use the format pattern for your required output.
Example:
DecimalFormat df = new DecimalFormat("#.####");
df.setRoundingMode(RoundingMode.CEILING);
for (Number n : Arrays.asList(12, 123.12345, 0.23, 0.1, 2341234.212431324)) {
Double d = n.doubleValue();
System.out.println(df.format(d));
}
gives the output:
12
123.1235
0.23
0.1
2341234.2125
EDIT: The original answer does not address the accuracy of the double values. That is fine if you don't care much whether it rounds up or down. But if you want accurate rounding, then you need to take the expected accuracy of the values into account. Floating point values have a binary representation internally. That means that a value like 2.7735 does not actually have that exact value internally. It can be slightly larger or slightly smaller. If the internal value is slightly smaller, then it will not round up to 2.7740. To remedy that situation, you need to be aware of the accuracy of the values that you are working with, and add or subtract that value before rounding. For example, when you know that your values are accurate up to 6 digits, then to round half-way values up, add that accuracy to the value:
Double d = n.doubleValue() + 1e-6;
To round down, subtract the accuracy.
Problems rounding to 2 decimal places
If you're going to do calculations with your decimal values, you really need to use BigDecimal
throughout. Never trust double
or float
to give accurate answers to calculations involving decimal values.
Also, when you create a BigDecimal
from a double
, don't use new BigDecimal(yourDouble)
, because that just puts the floating point error that's already in your double
into the BigDecimal
. It's generally much better to use BigDecimal.valueOf(yourDouble)
, which gives you the decimal value with the least number of decimal places that your double
is close enough to. That sounds bad, but it's actually good, because you typically get the value that was used to create the double
initially.
For example,
System.out.println(new BigDecimal(0.1)); // prints 0.1000000000000000055511151231257827021181583404541015625
System.out.println(BigDecimal.valueOf(0.1)); // prints 0.1
Related Topics
Execution Failed For Task :App:Compiledebugjavawithjavac in Android Studio
Distinguishing Between Java Threads and Os Threads
How to Generate Random Integers Within a Specific Range in Java
Add a Complex Image in the Panel, With Buttons Around It in One Customized User Interface
Why Are Only Final Variables Accessible in Anonymous Class
What Is the Volatile Keyword Useful For
How to Reverse an Int Array in Java
Why Does Gson Fromjson Throw a Jsonsyntaxexception: Expected Begin_Object But Was Begin_Array
Efficiency of Java "Double Brace Initialization"
How to Create Interface Between Fragment and Adapter
How to Test a Class That Has Private Methods, Fields or Inner Classes
How to Convert a Byte Array to a Hex String in Java
How to Download and Save a File from the Internet Using Java
Difference Between a Variable, Object, and Reference
Why Is a Concurrentmodificationexception Thrown and How to Debug It