High-precision Operation

Source: Internet
Author: User

In the development of high-precision operation is not very frequent, the precision of float and double can actually meet the overwhelming majority of needs, and high-precision operation is slower than the normal operation, but the use of high-precision operation is often used in the convenient API. The class of high precision operations provided by the Crown is mainly two

BigInteger: When the range of values exceeds long, use BigDecimal: almost cover the BigInteger function, you can also retain any number of decimal places, theoretically infinite, the following main explanation of this class
  
Note: Both APIs are similar

Subtraction

BigDecimal AAA = new BigDecimal (20);

BigDecimal bbb = new BigDecimal (3);

BigDecimal AAA. Add (BBB): an addition operation that returns the result of an operation

BigDecimal AAA. Subtract (BBB): subtraction, returns the result of an operation

BigDecimal AAA. Multiply (BBB): multiplication, returning result after operation

BigDecimal AAA. Divide (BBB): division operation, returning result after operation

Keep decimal points

Mainly for the division operation, there may be an infinite number of decimals, if not rounding operation will be an error

BigDecimal result = Aaa.divide (BBB, 8, BIGDECIMAL.ROUND_HALF_UP);

Parameter 1: Divisor

Parameter 2: Reserved precision, requires retention of several decimals

Parameter 3: Rounding mode, often several rounding methods

BIGDECIMAL.ROUND_HALF_UP: Rounding

Bigdecimal.round_ceiling: Rounding the upper limit value, such as the result of 1.1, returns 2

Bigdecimal.round_floor: Rounding lower values, such as 1.8, returns 1

Other APIs

* *value (): Convert to basic data type such as Aaa.intvalue (), Aaa.doublevalue (), etc.

BigInteger Tobiginteger (): Convert to BigInteger type

Bigdecimal.zero: Rep 0

Bigdecimal.one: Rep 1

Bigdecimal.ten: Rep 10

High-precision Operation

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.