I need to archive this: (basically num1 * num2 = num3 and num1 and num2 are given)
1) 10.62 * 4.0 = 42.48
2) 14.89 * 8.5 = 126.56
3) 13.21 * 6.0 = 79.26
4) 16.55 * 7.35 = 121.64
5) 18.62 * 9.0 = 167.58
6) 9.47 * 15.3 = 144.89
7) 6.58 * 3.0 = 19.74
8) 18.32 * 5.4 = 98.93
9) 12.15 * 2.9 = 35.24
10) 3.98 * 4.8 = 19.10
This exercise is to practice formatting/rounding string/number.
So I used:
DecimalFormat decimalFormat = new DecimalFormat(".00");
System.out.println(decimalFormat.format(num3));
and got everything right but the 9) 12.15 * 2.9 = 35.24
, I got 35.23
instead of 35.24
The default RoundingMode of DecimalFormat is HALF_EVEN, if I use RoundingMode.UP I will get this right 35.24 but the others results will be messed so I am stuck...
What should I use in order to get the expected result?
Using RoundingMode UNNECESSARY
, will solve your problem. Additionally you can catch exceptions and specify output manually if needed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.