Home  >  Article  >  Java  >  Java rounding and reserved bits sample code

Java rounding and reserved bits sample code

黄舟
黄舟Original
2017-08-22 09:56:471252browse

This article mainly introduces examples of rounding and reserved bits in Java. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.

Rounding is a mathematics problem in our elementary school. For us programmers, this problem is as simple as addition, subtraction, multiplication and division from 1 to 10. Before explaining, let’s look at the following classic case:


public static void main(String[] args) { 
    System.out.println("12.5的四舍五入值:" + Math.round(12.5)); 
    System.out.println("-12.5的四舍五入值:" + Math.round(-12.5)); 
  }

Output:

12.5 The rounding value of: 13

-The rounding value of 12.5: -12

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

Rounding is actually used a lot in finance, especially bank interest. We all know that the main profit channel of a bank is the interest difference. It collects funds from depositors and then lends them out. The interest difference generated during this period is the profit earned by the bank. If we adopt the usual rounding rules, here we use the calculation of interest for every 10 deposits as a model, as follows:

Rounding: 0.000, 0.001, 0.002, 0.003, 0.004. All this is money earned by the bank.

Five inputs: 0.005, 0.006, 0.007, 0.008, 0.009. These are the money lost by the bank, respectively: 0.005, 0.004, .003, 0.002, 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. It can be seen from the results that the bank may lose 0.005 yuan for every 10 interest payments. Don't underestimate this number. This is a very big loss for the bank. Faced with this problem, the following banker involvement method was born. This algorithm was proposed by American bankers and is mainly used to correct errors caused by the above rounding rules. As follows:

When the value of the rounding digit is less than 5, it will be rounded off directly.

When the value of the rounding digit is greater than 5, it will be rounded after rounding.

When the value of the rounding digit is equal to 5, if there are other non-0 values ​​after 5, it will be rounded off after carrying. If there is a 0 after 5, it will be based on the number of the digit before 5. Judgment is based on parity, odd numbers are rounded up, and even numbers are discarded.

Let’s give an example of the above rules

  • 11.556 = 11.56 ------Six-in

  • 11.554 = 11.55 -----Round up

  • 11.5551 = 11.56 -----Carry after five numbers

  • ##11.545 = 11.54 -----The number after five is countless, if the first digit is an even number, it should be discarded

  • 11.555 = 11.56 -----The number after five is countless, if the first digit is an odd number, it should be carried

The following example uses the banker's rounding method:



 public static void main(String[] args) { 
    BigDecimal d = new BigDecimal(100000);   //存款 
    BigDecimal r = new BigDecimal(0.001875*3);  //利息 
    BigDecimal i = d.multiply(r).setScale(2,RoundingMode.HALF_EVEN);   //使用银行家算法  
     
    System.out.println("季利息是:"+i); 
    }

Output:


The quarterly interest is: 562.50


The banker’s rounding method is briefly introduced above. Currently, Java supports 7 rounding methods:


1. ROUND_UP: stay away from zero Directional rounding. Rounding towards the maximum absolute value, as long as the discard bit is not 0, it is carried.


2. ROUND_DOWN: Rounding towards zero. Input in the direction with the smallest absolute value, all bits will be discarded, and there will be no carry.


3. ROUND_CEILING: Round towards positive infinity. Move 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 Math.round() method uses this pattern.


4. ROUND_FLOOR: Round towards negative infinity. Move closer to 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 nearest number is rounded (into 5). This is our most classic roundup.


6. HALF_DOWN: The nearest number is rounded (5 rounding). Here 5 is to be discarded.


7. HAIL_EVEN: Banker's rounding method.


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


Reserved bits

Method 1: Rounding


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

Use BigDecimal here, and use the setScale method to set the accuracy, and use RoundingMode.HALF_UP to indicate using the nearest number rounding rule for approximate calculation. Here we can see that BigDecimal and rounding are a perfect match.


Method 2:


java.text.DecimalFormat  df  =new  java.text.DecimalFormat(”#.00″); 
df.format(你要格式化的数字);

Example:

new java.text.DecimalFormat(”#.00″). format(3.1415926)

.00 means two decimal places, #.0000, four decimal places, and so on...


Method 3:


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

%.2f %. Represents any number of digits before the decimal point 2 Represents the result after two decimal places format f Represents a floating point type.


Method 4:

In addition, if you use struts tags for output, there is a format attribute, set to format="0.00" to keep it Two decimal places


例如: 


<bean:write name="entity" property="dkhAFSumPl" format="0.00" /> 
//或者 
<fmt:formatNumber type="number" value="${10000.22/100}" maxFractionDigits="0"/>

maxFractionDigits表示保留的位数 

The above is the detailed content of Java rounding and reserved bits sample code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn