What does Half_even mean? If the number you want to throw away is not 5, you need to look at the parity of the number in front of this number, if it is odd, use the half_up rule, if it is even use the Half_down rule, if the number you want to discard is 5, round to its even neighbor;
What does it mean to round to its even-numbered neighbors?
Example:
System.out.println ("=================="); for (int i = 0; i < ten; i + +) {StringBuffer sb = new StringBuffer (); Sb.append (i); Sb.append (". 5"); BigDecimal BDX = new BigDecimal (sb.tostring ()); SYSTEM.OUT.PRINTLN (SB + "" +bdx.setscale (0, Roundingmode.half_even));} System.out.println ("=================="); for (int i = 0; i < ten; i + +) {StringBuffer sb = new StringBuffer (); Sb.append ("0."); Sb.append (i); Sb.append ("5"); BigDecimal BDX = new BigDecimal (sb.tostring ()); SYSTEM.OUT.PRINTLN (SB + "" +bdx.setscale (1, Roundingmode.half_even));}
The output is:
==================
0.5 0-->0.5 Distance 0 and 1 are 0.5, but 0 is even, all round to 0
1.5 2
2.5 2
3.5 4
4.5 4
5.5 6
6.5 6
7.5 8
8.5 8
9.5 10
==================
0.05 0.0
0.15 0.2
0.25 0.2
0.35 0.4
0.45 0.4
0.55 0.6
0.65 0.6
0.75 0.8
0.85 0.8
0.95 1.0
BigDecimal in Java Half_even