Bigdecimal.setscale processing Java decimal point and rounding algorithm __ algorithm

Source: Internet
Author: User
The Bigdecimal.setscale () method is used to format the decimal point
Setscale (1) means to keep a decimal number by default by rounding
Setscale (1,bigdecimal.round_down) directly remove the extra decimal places, such as 2.35 will become 2.3
Setscale (1,BIGDECIMAL.ROUND_UP) carry handle, 2.35 becomes 2.4
Setscale (1,bigdecimal.round_half_up) rounded, 2.35 became 2.4 setscaler (1,bigdecimal.round_half_down) rounded, 2.35 turns 2.3, if it's 5, it's down. Note:1:scale refers to the number of digits after your decimal point. For example 123.456 score is 3.
Score () is the method in the BigDecimal class.
For example: BigDecimal B = new BigDecimal ("123.456"); B.scale (), the return is 3.
2:
Roundingmode is a retention pattern for decimals. They are all constant fields in BigDecimal, and there are many kinds.
For example: BIGDECIMAL.ROUND_HALF_UP says is 4 homes 5.
3:
Pubilc BigDecimal Divide (BigDecimal divisor, int scale, int roundingmode)
I use a BigDecimal object divided by the result of divisor, and I want this result to keep a scale decimal place, roundingmode means what the retention mode is, rounded or otherwise, you can choose for yourself.

4: For the general Add, subtract, multiply method of decimal format is as follows: BigDecimal mdata = new BigDecimal ("9.655"). Setscale (2, BIGDECIMAL.ROUND_HALF_UP);
System.out.println ("mdata=" + mdata);
----Results:-----mdata=9.66

Http://hi.baidu.com/hsyl/blog/item/5a690df4b546a6e57709d7a5.html

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.