In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Java how to round and round, for this problem, this article details the corresponding analysis and solution, hoping to help more small partners who want to solve this problem to find a simpler and easier way.
Rounding is our elementary school math problem, which for us programmers is similar to the addition, subtraction, multiplication and division of 1 to 10. Let's look at a classic example:
public static void main(String[] args) { System.out.println("rounding of 12.5:" + Math.round(12.5)); System.out.println("-12.5 rounded: " + Math.round(-12.5)); } Rounding of Output:12.5: 13-Rounding of 12.5:-12
This is a classic case of rounding, and it is also something we often encounter when we participate in school recruitment (it seems that I have encountered many times when I participate in written examinations). From this result we find that these two numbers have the same absolute value. Why do the approximate values differ? In fact, this is determined by the rounding rule adopted by Math.round.
Rounding is actually used a lot in finance, especially bank interest. We all know that the bank's profit channel is mainly the interest difference. It collects funds from depositors and then lends them out. The interest difference generated during the period is the profit earned by the bank. If we adopt the usual rounding rule, we use the interest calculation for every 10 deposits as the model, as follows:
Rounding: 0.000, 0.001, 0.002, 0.003, 0.004. All of this is money earned by banks.
Five entries: 0.005, 0.006, 0.007, 0.008, 0.009. These are all bank losses: 0.005, 0.004,.003, 0.002, 0.001.
So 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. It can be seen from the results that every 10 interest banks may lose 0.005 yuan, do not underestimate this figure, this is a very large loss for banks. Faced with this problem arises the following banker involvement. The algorithm was developed by American bankers to correct errors caused by the rounding rule above. As follows:
If the value of the truncated bit is less than 5, it is directly truncated.
When the number of truncated bits is greater than 5, it is truncated after rounding.
When the value of the truncated bit is equal to 5, if there are other non-zero values after 5, carry them off; if 5 is followed by 0, judge according to the parity of the first digit of 5, odd carry, even number off. For the above rules we illustrate
11.556 = 11.56 -----Six entries
11.554 = 11.55 ----rounding
11.5551 = 11.56 -----There is a number carry after the fifth
11.545 = 11.54 ----There are countless numbers after the fifth, if the preceding digits are even numbers, they should be discarded.
11.555 = 11.56 ----There are countless numbers after five, if the leading digit is odd, carry it
The following example uses banker rounding:
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 banker algorithm System.out.println("quarterly interest is: "+i); }Output: Quarterly interest is: 562.50
Banker rounding was briefly introduced above, and Java currently supports rounding in 7:
ROUND_UP: Round away from zero. Rounding in the direction of the largest absolute value, as long as the discard bit is not 0 or carry.
ROUND_DOWN: Rounding toward zero. Input in the direction of minimum absolute value, all bits are discarded, there is no carry case.
ROUND_CEILING: Rounding to positive infinity. Heading towards positive maximum. If positive, rounding behavior is similar to ROUND_UP, if negative, rounding behavior is similar to ROUND_DOWN. The Math.round() method is used in this pattern.
ROUND_FLOOR: Round to negative infinity. Towards negative infinity. If positive, rounding behavior is similar to ROUND_DOWN; if negative, rounding behavior is similar to ROUND_UP.
HALF_UP: Rounds the nearest digit (to 5). This is our most classic rounding.
6. HALF_DOWN: Rounding off the nearest digit (rounded off by 5). Here 5 is to be abandoned.
HAIL_EVEN: Banker rounding.
When it comes to rounding, reserved bits are essential, and there are many ways to implement reserved bits in java computing.
Reserved Bit Method 1: Round 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, while RoundingMode.HALF_UP is used to approximate calculations using the nearest number rounding rule. Here we can see that BigDecimal and rounding are a wonderful combination.
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 for two decimal places #.0000 for four 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 means two-digit decimal format and the result is f means floating point. Method 4:
In addition, if you use the struts tag for output, there is a format attribute, set to format="0.00" to retain two decimal places.
For example:
Or maxFractionDigits indicates the number of reserved digits. The answers to Java rounding and rounding questions are shared here. I hope the above content can be helpful to everyone. If you still have a lot of doubts, you can pay attention to the industry information channel to learn more.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.