# Java-bigdecimal Analysis

Source: Internet
Author: User

Introduction

recently looking at the various calculations in the project, as is the financial aspect of the project, involving the daily,years of, interest, debt to these and money related to the calculation of a lot, which all through the BigDecimal class of operations, previously involved in the calculation of the basic use of +,-, *,/and other arithmetic operators to perform mathematical operations directly, the data type is generally usedDoublecan solve the problem, but if full precision is requiredDoubleIt doesn't fit, becauseDoubleis to provide a more accurate, fast approximation on a wide range of values (only processing -bit valid number).

BigDecimalis aJavain theJava.mathprovided in the packageAPIclass,Mainused to over -The number of bit-effective bits for precise operation. Double-precision floating-point variableDoublecan handle -The number of bits valid. In practical applications, larger or smaller numbers need to be calculated and processed. floatand theDoubleIt can only be used for scientific calculations or engineering calculations, in business calculations .Java.math.BigDecimal.

BigDecimal The object is created and we cannot use the traditional + , - , * , / The arithmetic operators are mathematically operating directly on their objects, and must call their corresponding methods. The arguments in the method must also be BigDecimal objects. Here's how to construct the BigDecimal .

Construction Method

BigDecimal (int) Creates an object with the integer value specified by the parameter.

BigDecimal (double) creates an object with the double value specified by the parameter.

BigDecimal (long) creates an object with a long integer value specified by the parameter.

BigDecimal (String) creates an object that has the numeric value specified by the parameter as a string.

It's important to note that Double type, let's look at an example:

`<pre name= "code" class= "Java" >//1, BigDecimal (double) BigDecimal adouble =new BigDecimal (1.1); System.out.println ("to double:" + adouble); 2, BigDecimal (String) BigDecimal astring = new BigDecimal ("1.1"); System.out.println ("to string:" + astring);//3, bigdecimal.valueof (double) BigDecimal avalue = bigdecimal.valueof (1.1) ; System.out.println ("valueOf:" + avalue);`

Output Result:

`To Double:1.100000000000000088817841970012523233890533447265625to string:1.1valueof:1.1`

1 Here we notice that the parameter is double 1.100000000000000088817841970012523233890533447265625 1.1 1 1 cannot be accurately represented as a double (or for that case, it cannot be represented as any finite-length binary decimal).

2. The BigDecimal value constructed with the argument string type is exactly equal to the original value 1.1 . Therefore, when we construct the BigDecimal value, we typically select a constructor that has a parameter of type string.

3. When double must be used as a parameter to BigDecimal, you can use the double.tostring (double) method to convert a double to a string, and then use the BigDecimal (String) to construct the method. Then use the bigdecimal.valueof (double) method to get the value.

operation mode

Subtraction the most basic operations

BigDecimal Subtract (BigDecimal subtrahend) subtraction operation

BigDecimal Multiply (BigDecimal multiplicand) multiplication operation

BigDecimal Divide (BigDecimal divisor) Division operation

Because the pursuit of high precision, we use BigDecimal, but when the operation with Division, and the results need to retain a significant number of digits, there may be a precision problem. When will it appear?

We know that 10 except 3 is always endless, and as a result we need to format (preserve the number of significant digits)

precision and Retention modes

There is no limit to the precision of BigDecimal objects. For an endless division operation, such as the 10/3,divide method, the Java.lang.ArithmeticException error is thrown, so the division operation should try to use divide (BigDecimal d, int scale, int Roundmode) specify scale and retention mode to avoid exceptions.

scale, scales, number of digits reserved for the decimal point

Roundmode, retention mode

Bigdecimal.round_down: Drop the decimal outside the scale directly

BIGDECIMAL.ROUND_UP: No matter how many decimals you discard, go 1.

BIGDECIMAL.ROUND_HALF_UP: The most common rounding

There are other models, you can go directly to the source to see.

Consider the example calculation:

`<pre name= "code" class= "Java" >bigdecimal a =new BigDecimal ("10"); BigDecimal C = a.divide (new BigDecimal (3), 10,bigdecimal.round_down); BigDecimal d = c.multiply (new BigDecimal (3)); System.out.println ("Value:" + C); System.out.println ("value:" + D);`

The output is:

value:3.3333333333
value:9.9999999999

Because the division of the first step specifies the scale and retention mode, we get an approximate value, has lost the precision, in the subsequent operation, the accuracy is not guaranteed, how to solve this problem? The division operation should be placed at the end of the entire operation in order to reduce the precision problem caused by division.

Summary

Business computing is most sensitive to money, a penny usually seems very small, but placed in the million,billion,everyhundred loss a cent, the result is huge. Learning is the same, learning a point every day, a year after the change is also huge.

Java-bigdecimal Analysis

Related Keywords:

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.