Java Improved-----Detailed Java rounding and retention bits

Source: Internet
Author: User

Rounding is a mathematical problem in our primary school, which is as simple as the 1 to 10 subtraction for our program ape. Let's look at one of the following classic examples:

public static void Main (string[] args) {        System.out.println ("Rounding Value of 12.5:" + math.round (12.5));        System.out.println (rounded value of "-12.5:" + Math.Round ( -12.5));    } Rounding value of output:12.5:13-12.5 Rounding Value: 12

It's a classic case of rounding, and it's something that we often encounter when we take part in the school enrollment (it seems like I've met many times during the written test). From here, we find that the two numbers with the same absolute value, why are the approximate values different? In fact, this is determined by the rounding rule used by Math.Round.

Rounding is actually very much used in finance, especially in the interest of banks. We all know that the bank's profit channel is mainly the interest rate is poor, it collects funds from the depositors ' hands, then lends out, during the period produces the interest difference is the bank obtains the profit. If we use regular rounding rules, we use each of the 10 deposit interest calculations as a model, as follows:

Four homes: 0.000, 0.001, 0.002, 0.003, 0.004. These are the money that the banks make.

Five in: 0.005, 0.006, 0.007, 0.008, 0.009. These are the banks that are losing money: 0.005, 0.004,. 003, 0.002, 0.001, respectively.

So for a bank it 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 can be seen every 10 interest bank may lose 0.005 yuan, do not underestimate this figure, which for the bank is a very big loss. Faced with this problem arose the following banker rounding method. The algorithm was proposed by the American banker and was mainly used to correct the error caused by the rounding rule above. As follows:

The value of the shed bit is less than 5 o'clock, and is directly shed.

The value of the shed bit is greater than 5 o'clock, and the rounding is rounded off.

When the value of the bit is equal to 5 o'clock, if there are other non-0 values after 5, then carry the back, if 5 is 0 o'clock, then according to the parity of the 5 previous one number to judge, odd carry, even shed.

For the above rules, we illustrate

11.556 = 11.56------Six in

11.554 = 11.55-----Four Homes

11.5551 = 11.56-----Five Post

11.545 = 11.54-----Five after the countless, if the former is an even-numbered should be shed

11.555 = 11.56-----After five innumerable, if the first bit is an odd number should carry

The following example uses 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 = d.multiply (R). Setscale (2,roundingmode.half_even);     Using the Banker algorithm                 System.out.println ("Quarterly interest is:" +i);        } Output: Quarterly interest is: 562.50

Here is a brief introduction to the banker rounding method, currently supported in Java 7 Rounding Method:

1, Round_up: Rounding away from 0 directions. Rounds in the direction of the absolute value, as long as the discard bit is not 0.

2, Round_down: Trend 0 Direction rounding. Enter in the direction of the smallest absolute value, all the bits are discarded, there is no rounding.

3, Round_ceiling: Rounding to positive infinity. To the positive maximum direction. If positive, the rounding behavior is similar to round_up, and if negative, the rounding behavior is similar to Round_down. The Math.Round () method is the use of this pattern.

4. Round_floor: Rounding in negative infinity direction. To the negative Infinity direction. If positive, the rounding behavior is similar to Round_down, and if negative, the rounding behavior is similar to ROUND_UP.

5, Half_up: The nearest number is rounded (5 in). This is our most classic rounding.

6, Half_down: The most recent number rounding (5 homes). Here 5 is to abandon.

7, Hail_even: Banker rounding Method.

It is necessary to mention rounding so that reserved bits are used, and in Java operations we can implement the reserved bits in many ways.

Reserved bits

Method One: Rounding

Double   f   =   111231.5585; BigDecimal   B   =   new   BigDecimal (f);d ouble   f1   =   B.setscale (2,   roundingmode.half _UP). Doublevalue ();

Use BigDecimal here, and use the Setscale method to set the accuracy, while using the roundingmode.half_up notation to approximate the calculation using the nearest number rounding rule. Here we can see that bigdecimal and rounding are wonderful pairings.

Way two:

Java.text.DecimalFormat   DF   =new   java.text.DecimalFormat ("#.00″");d F.format (the number you want to format);

Example: New Java.text.DecimalFormat ("#.00″"). Format (3.1415926)

#.00 represents two decimal places #.0000 four decimal places and so on ...

Way three:

Double d = 3.1415926; string result = string. Format ("%.2f");%.2f%. Indicates that any number of digits before the decimal point   of 2 means that the result of the two-bit decimal format is f for floating point.

Mode four:

In addition, if you use the Struts label for output, there is a Format property, set to Format= "0.00" is reserved 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 bits

Reprinted from: http://blog.csdn.net/chenssy/article/details/12719811

Java Improved-----Detailed Java rounding and retention bits

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.