(Math.round(doubleValue*100))/100.0
Is there a better way to round decimals to 2 decimal places?
If you're interested in decimal places and therefore precise decimal values, you should typically be using java.math.BigDecimal to start with. You can then use Decimal.round or Decimal.setScale to round according to your exact needs.
DecimalFormat format=new DecimalFormat("#.##");
System.out.println(format.format(doubleValue));
Related
I want to know if there is any advantage of using BigDecimal RoundingMode method over DoubleRounder.round().
Also I observe the differences in the result when I try to use the same precision in two methods. For Example :
Using DoubleRounder.round()
System.out.println(""+DoubleRounder.round(0.0897435897435897, 5)); //gives 0.08974
Using BigDecimal RoundingMode
BigDecimal b= new BigDecimal(0.0897435897435897);
System.out.println(""+b.round(new MathContext(5,RoundingMode.CEILING)); //gives 0.089744
I see that there are 6 digits in decimal places while using BigDecimal instead of 5. Can anyone explain me why this is happening. And in case I want to have exact 5 digit after decimal using BigDecimal, how do I need to modify this part of code?
MathContext accept precision, it means starts from the leftmost nonzero digit.
you can use bigDecimal.setScale(5, RoundingMode.CEILING).
scale is the number of digits to the right of the decimal point.
e.g.
System.out.println(b.setScale(5, RoundingMode.CEILING)); // gives you 0.08975
It will give you 5 digit
System.out.println(""+b.round(new MathContext(4,RoundingMode.CEILING))); //gives 0.08975
Right now I am doing this
DecimalFormat df = new DecimalFormat("#.##");
//usage: df.format(someDouble);
But I don't know if this is the correct way to "round off" the display of a double to 2 decimal points. Will this work even if the number of digits to the left of the decimal point > 1?
You should use:
DecimalFormat df = new DecimalFormat("#.00");
DecimalFormat df = new DecimalFormat("#.##"); means, that those decimal places are optional. You can see the difference when applying on 1.2 or 1.0.
--Documentation
To answer the question about the rounding, the documentation tell it :
Rounding
DecimalFormat provides rounding modes defined in RoundingMode for formatting. By default, it uses RoundingMode.HALF_EVEN.
So this is up to you to set the RoundingMode that you want with the setter.
public void setRoundingMode(RoundingMode roundingMode)
Will this work even if the number of digits to the left of the decimal point > 1?
Yes it will.
I would like to print a BigDecimal in scientific notation so that the there are, say, 5 digits after the decimal point. For example: -3.12345E-51, 9.12345E100
.setScale() does not work in this case, because I don't know in advance the exponent of number in scientific notation. Moreover, BigDecimal doesn't seem to have a getExponent() method.
What's the best way to achieve this? Thank you in advance.
You should use DecimalFormat to print numbers in specified format:
BigDecimal bigDecimal = new BigDecimal(-3.12345E-51);
DecimalFormat format = new DecimalFormat("0.#####E0");
System.out.println(format.format(bigDecimal));
Also you can configure count of fraction digits using method DecimalFormat#setMaximumFractionDigits(int):
DecimalFormat format = new DecimalFormat("0E0");
format.setMaximumFractionDigits(6);
I am trying to format percentages with the following code:
NumberFormat fmt = NumberFormat.getPercentInstance();
fmt.setRoundingMode(RoundingMode.HALF_UP);
fmt.setMinimumFractionDigits(0);
fmt.setMaximumFractionDigits(0);
System.out.println(fmt.format(0.145));
However, I get a very strange result:
14%
Changing the value 0.145 to something else, for example 0.125 will work properly and the result will be as expected
13%
Can someone shed some light on this? Thanks in advance
This is due to the inherent rounding error in double, resulting in 0.145 being rounded to
0.1449999999999999900079927783735911361873149871826171875
Use BigDecimal if you expect perfect accuracy.
I think that is an result of the intern representation of floats. (Decimal numbers represented by binary numbers).
Your 0.145 might be internally represented as 0.144999999999..., so rounding mode rounds down.
You should read What Every Computer Scientist Should Know About Floating-Point Arithmetic, but basically it comes down to the fact that .145 can't be exactly represented in IEEE floating point, and so gets rounded to the nearest value that can be represented, which just happens to be slightly less that .145, so it gets rounded down when rounded to two digits.
I'm trying to format a double to just 2 decimal places in Java.
I've read the following post:
How to round a number to n decimal places in Java
For some reason, every one of those methods fails to work on certain numbers I have..
For example:
DecimalFormat df = new DecimalFormat("#.##");
normalizedValue = Double.valueOf(df.format(normalizedValue));
If I print normalizedValue I get result similar to the following:
-78.64000000000001
18.97
59.469999999999985
-63.120000000000005
(Note: some are formatted correctly... some aren't)
So, these methods seem to round, but I need something that will remove all decimals after 2 decimal places... Any suggestions?
Thank you.
The string representation given by DecimalFormat.format(...) does indeed have only 2 digits in your example. But as soon as you reconvert it into a double those accuracy issues occur. A binary base format like double will always show such effects when you try to represent "exakt" decimal fractions. You may change to BigDecimal to get rid of such effects.