Java improvements ----- detailed explanation of java rounding and retaining bits, ----- rounding
Rounding out is a mathematical problem in our primary school. This problem is as simple as the addition, subtraction, multiplication, and division of 1 to 10. Let's take a look at the next typical case:
Public static void main (String [] args) {System. out. println ("12.5 rounding value:" + Math. round (12.5); System. out. println ("-12.5 rounding value:" + Math. round (-12.5);} Output: 12.5 rounding value:-12
This is a classic case of rounding up and down, which we often encounter when taking school recruitment (it seems that I have met many times when I took the test ). From the results, we find that the two numbers with the same absolute values have different approximate values? In fact, this is determined by the rounding rules adopted by Math. round.
In fact, it is used in a lot of financial aspects, especially the interest of banks. We all know that the bank's profit channel is mainly interest difference. It collects funds from the depositor and then lends money. The interest difference generated during the period is the profit obtained by the bank. If we use a general Rounding Rule, we use the calculation of every 10 deposits as the model, as shown below:
Four Homes: 0.000, 0.001, 0.002, 0.003, 0.004. All these houses are made by banks.
5: 0.005, 0.006, 0.007, 0.008, 0.009. All of these funds are due to bank losses: 0.005, 0.004,. 003, 0.002, and 0.001.
Therefore, for a bank, its profit should be 0.000 + 0.001 + 0.002 + 0.003 + 0.004-0.005-0.004-0.003-0.002-0.001 =-0.005. From the results, we can see that every 10 Interest banks may lose 0.005 yuan. Don't underestimate this number, which is a huge loss for banks. In the face of this problem, the following banker rounding method is created. This algorithm was proposed by American bankers and is mainly used to correct the errors produced by rounding the rules above. As follows:
If the value of the decrement is less than 5, it is directly removed.
When the value of the rounded decimal place is greater than 5, it is carried back.
When the rounding value is equal to 5, if there are other non-0 values after 5, the carry value is left behind. If the value behind 5 is 0, the result is determined based on the parity of the first digit of 5, returns an odd carry number.
Examples of the above rules
11.556 = 11.56 ------ six inputs
11.554 = 11.55 ----- four homes
11.5551 = 11.56 ----- carry-in after five
11.545 = 11.54 ----- countless digits after five. If the front digit is an even number, it should be removed.
11.555 = 11.56 ----- countless digits after five. If the prefix is an odd number, it should be carried.
The following example uses the banner rounding method:
Public static void main (String [] args) {BigDecimal d = new BigDecimal (100000); // deposit BigDecimal r = new BigDecimal (0.001875*3 ); // interest BigDecimal I = d. multiply (r ). setScale (2, RoundingMode. HALF_EVEN); // use the Banker algorithm System. out. println ("quarterly interest:" + I);} Output: quarterly interest: 562.50
The banner rounding method is briefly introduced above. Java currently supports the Rounding Method in 7:
1. ROUND_UP: Rounding away from zero. Round to the maximum direction of the absolute value, as long as the given bit is not 0, it is carried.
2. ROUND_DOWN: rounded to zero. Input in the direction with the smallest absolute value. All bits must be discarded. There is no carry condition.
3. ROUND_CEILING: round to positive infinity. Move closer to the positive and maximum directions. If it is a positive number, the rounding behavior is similar to ROUND_UP. If it is a negative number, the rounding behavior is similar to ROUND_DOWN. The Math. round () method uses this mode.
4. ROUND_FLOOR: round to negative infinity. Move closer to the negative infinity direction. If it is a positive number, the rounding behavior is similar to ROUND_DOWN; if it is a negative number, the rounding behavior is similar to ROUND_UP.
5. HALF_UP: round the nearest number (step 5 ). This is our most classic rounding.
6. HALF_DOWN: round the nearest number (five homes ). Here, 5 is to be discarded.
7. HAIL_EVEN: bankers rounding.
When rounding is mentioned, the reserved bits are essential. In java operations, we can use multiple methods to implement the reserved bits.
Reserved Bit
Method 1: Rounding
double f = 111231.5585;BigDecimal b = new BigDecimal(f);double f1 = b.setScale(2, RoundingMode.HALF_UP).doubleValue();
BigDecimal is used here, And the setScale method is used to set the precision. At the same time, RoundingMode. HALF_UP indicates that the nearest number Rounding Rule is used for approximate calculation. Here we can see that BigDecimal and rounding are wonderful matches.
Method 2:
Java. text. DecimalFormat df = new java. text. DecimalFormat ("#. 00"); df. format (the number you want to format );
Example: new java. text. DecimalFormat ("#. 00"). format (3.1415926)
#. 00 indicates two decimal places #. 0000 four decimal places, and so on...
Method 3:
Double d = 3.1415926; String result = String. format ("%. 2f "); %. 2f %. 2 indicates the number of digits before the decimal point. The result of two decimal places is f, indicating the floating point type.
Method 4:
In addition, if the struts label is used for output, there is a format attribute, set to format = "0.00" is to keep two decimal places
For example:
<Bean: write name = "entity" property = "dkhAFSumPl" format = "0.00"/> or <fmt: formatNumber type = "number" value = "$ {10000.22/100}" maxFractionDigits = "0"/> maxFractionDigits indicates the number of reserved digits
Reprinted from: http://blog.csdn.net/chenssy/article/details/12719811