SoFunction
Updated on 2025-03-06

Detailed explanation of rounding and reserved bit examples of java

Rounding is a mathematical problem in our primary school. This problem is as simple as addition, subtraction, multiplication and division of 1 to 10 for us programmers. Let’s first look at the following classic case:

public static void main(String[] args) { 
    ("Rounded value of 12.5:" + (12.5)); 
    ("Rounded value of -12.5:" + (-12.5)); 
  } 

Output: 

Rounding value of 12.5: 13

-12.5 rounded value: -12

This is a classic case of rounding, and it is also something we often encounter when we participate in campus recruitment (it seems that I have encountered it many times when I took the written test). From the results here, we find that these two numbers with the same absolute value, why are the approximate values ​​different? In fact, this is determined by the rounding rules adopted.

Rounding is actually used a lot in finance, especially bank interest. We all know that the profit channel of banks is mainly interest spreads. They collect funds from depositors and then lend them out. The interest spread generated during the period is the profits obtained by banks. If we use the usual rounding rule, we use the calculation of interest per 10 deposits as the model, as follows:

Four rows: 0.000, 0.001, 0.002, 0.003, 0.004. These are all the money earned by the bank.

Five entry: 0.005, 0.006, 0.007, 0.008, 0.009. These money lost by the bank is: 0.005, 0.004, .003, 0.002, and 0.001.

So for the 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, it can be seen that the bank may lose 0.005 yuan for every 10 interest transactions. Don’t underestimate this number, as this is a very large loss for the bank. Faced with this problem, the following banker involvement law has emerged. This algorithm was proposed by American bankers and is mainly used to correct the errors caused by using the above rounding rule. as follows:

If the value of the abandoned position is less than 5, just give it away.

When the value of the abandoned position is greater than 5, it will be abandoned after carrying.

When the value of the abandoned bit is equal to 5, if there are other non-0 values ​​after 5, then the carry is discarded after the carry. If 5 is followed by 0, then the odd number is judged based on the parity of the previous digit of 5, and the odd number is carried and the even number is discarded.

Let's give examples of the above rules

  • 11.556 = 11.56 -------Six
  • 11.554 = 11.55 -----Six
  • 11.5551 = 11.56 ----There are numbers after five
  • 11.545 = 11.54 ------The number after five is countless. If the previous position is an even number, you should leave it
  • 11.555 = 11.56 ------------------------------------------------------------------------------------------------------------------

The following example is using the banker rounding method:

 public static void main(String[] args) { 
    BigDecimal d = new BigDecimal(100000);   //deposit    BigDecimal r = new BigDecimal(0.001875*3);  //Interest    BigDecimal i = (r).setScale(2,RoundingMode.HALF_EVEN);   //Use banker algorithm     
    ("Quarterly interest is:"+i); 
    } 

Output: 

Quarterly interest is: 562.50

The banker rounding method is briefly introduced above. Currently, Java supports rounding method in 7:

1. ROUND_UP: Round away from zero direction. Round in the direction with the greatest absolute value, as long as the bit is discarded, it is carried.

2. ROUND_DOWN: Rounding in the direction of zero. Enter in the direction with the smallest absolute value, and all bits must be discarded, and there is no carry situation.

3. ROUND_CEILING: Round in the direction of positive infinite. Go closer to the positive maximum direction. 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 () method is to use this mode.

4. ROUND_FLOOR: Round in the direction of negative infinity. Go closer to the direction of negative infinity. 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: The most recent number is rounded (5 in). This is our most classic rounding.

6. HALF_DOWN: The most recent number rounded (5 rounds). Here 5 is to be abandoned.

7. HAIL_EVEN: banker rounding method.

When it comes to rounding, reserved bits are essential. In Java operations, we can use a variety of ways to implement reserved bits.

Reservation bit

Method 1: Round

double  f  =  111231.5585; 
BigDecimal  b  =  new  BigDecimal(f); 
double  f1  =  (2,  RoundingMode.HALF_UP).doubleValue(); 

Here, BigDecimal is used, and the setScale method is used to set the accuracy, while RoundingMode.HALF_UP is used to indicate that the most recent number rounding rule is used to approximate the calculation. Here we can see that BigDecimal and rounding are a wonderful combination.
Method 2:

  df  =new  (”#.00″); 
(The number you want to format); 

example:new (”#.00″).format(3.1415926)

#.00 represents two decimal places #.00004 decimal places and so on…

Method 3:

double d = 3.1415926; 
 
String result = String .format(”%.2f”); 

%.2f %. Indicates any number of digits before the decimal point. 2 indicates two decimal places. The result after format is f indicates floating point type.

Method 4:

In addition, if you use the struts tag for output, there is a format property, which sets to format="0.00" to retain 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 represents the number of retained digits

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.