Home >Java >javaTutorial >Java rounding and reserved bits sample code
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
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:
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)
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:
例如:
<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!