The number of decimal places can be set.

Source: Internet
Author: User
public static double round(double value, int scale,int roundingmode) { BigDecimal bd = new BigDecimal(value); bd = bd.setScale(scale,roundingmode); double d = bd.doubleValue(); bd = null; return d; }

Scale indicates the number of decimal places, and roundingmode indicates the trade-off method, such as rounding.

The value of roundingmode is as follows:

ROUND_CEILING
Rounding mode to round towards positive infinity.
Round to positive infinity

ROUND_DOWN
Rounding mode to round towards zero.
Round to zero

ROUND_FLOOR
Rounding mode to round towards negative infinity.
Round to negative infinity

ROUND_HALF_DOWN
Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.
Round to the nearest side, unless the two sides (distance) are equal. If so, round down. For example, 1.55 retains a decimal number and returns 1.5.

ROUND_HALF_EVEN
Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.
Round to the nearest side (distance), unless the two sides (distance) are equal. If so, use round_half_up if the reserved digits are odd. If the reserved digits are even, use round_half_down.

Round_half_up
Rounding mode to Round towards "Nearest Neighbor" unless both neighbors are equidistant, in which case round up.
Round to the nearest side (distance), unless the two sides (distance) are equal, if so, round up, 1.55 retains a decimal number and returns 1.6

Round_unnecessary
Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.
The calculation result is accurate, and the rounding mode is not required.


Round_up
Rounding mode to round away from zero.
Round to the direction away from 0



The BigDecimal. setScale () method is used to format the decimal point. The BigDecimal class is used for precise decimal operations, such as decimal operations for commercial purposes.
SetScale (1) indicates that one decimal point is retained. By default, the rounding method is used.
SetScale (1, BigDecimal. ROUND_DOWN) directly deletes unnecessary decimal places. For example, 2.35 will change to 2.3.
SetScale (1, BigDecimal. ROUND_UP) carry processing, 2.35 to 2.4
SetScale (1, BigDecimal. ROUND_HALF_UP) rounded to 2.35, 2.4 to 2.35 setScaler (1, BigDecimal. ROUND_HALF_DOWN) rounded to 2.3, to, and 5 to bottom Note:1: scale refers to the number of digits after the decimal point. For example, 123.456 means score is 3.
Score () is the method in the BigDecimal class.
For example, BigDecimal B = new BigDecimal ("123.456"); B. scale (), 3 is returned.
2:
RoundingMode is the retention mode of decimal places. They are all constant fields in BigDecimal, and there are many types.
For example, BigDecimal. ROUND_HALF_UP indicates 4 to 5.
3:
Pubilc BigDecimal divide (BigDecimal divisor, int scale, int roundingMode)
The result of dividing a BigDecimal object by the divisor is required to retain the result with a scale decimal place. roundingMode indicates the retention mode, whether it's rounding off or other items. You can choose one by yourself!

4: The decimal places of the add, subtract, and multiply methods are formatted as follows:

Bigdecimal mdata = new bigdecimal ("9.655"). setscale (2, bigdecimal. round_half_up );
System. Out. println ("mdata =" + mdata );
  ---- Result: ----- mdata = 9.66


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.