Java uses the BigDecimal class to handle high-precision computations

Source: Internet
Author: User

Java provides the API class BigDecimal in the Java.math package to perform precise operations on the number of more than 16 significant bits. Double-precision floating-point variable double can handle 16-bit valid numbers, but in practical applications, larger or smaller numbers may need to be calculated and processed. In general, for those numbers that do not require accurate calculation accuracy, we can use float and double processing directly, but double.valueof (string) and float.valueof (string) lose precision. So in development, if we need to calculate the results accurately, we must use the BigDecimal class to operate!

(i) Several common construction methods of the BigDecimal class

    • BigDecimal (int): Converts an int representation to a BigDecimal object
    • BigDecimal (String): Converts a string representation to a BigDecimal object
    • BigDecimal (double): Converts a double representation to a BigDecimal object

(ii) Common methods of BigDecimal class

  • Add (BigDecimal): Adds the values from the BigDecimal object and returns the BigDecimal object
  • Subtract (BigDecimal): Subtracts the value from the BigDecimal object and returns the BigDecimal object
  • Multiply (BigDecimal): Returns the BigDecimal object by multiplying the values in the BigDecimal object
  • Divide (BigDecimal): Divide the value in the BigDecimal object to return the BigDecimal object
  • ToString (): Converts the value in the BigDecimal object to a string
  • Doublevalue (): Converts a value in a BigDecimal object to a double-precision number
  • Floatvalue (): Converts a value in a BigDecimal object to a single-precision number
  • Longvalue (): Converts the values in the BigDecimal object to grow integers
  • Intvalue (): Converts the value in the BigDecimal object to an integer
  • Share the Java code below for reference
  • 1 Java code Copy Code collection code2 ImportJava.math.BigDecimal; 3 /**  4 * Because Java's simple type does not accurately operate on floating-point numbers, this tool class provides fine5 * Accurate floating-point arithmetic, including subtraction and rounding. 6 */  7  Public classarith{//Default division operation Precision8 Private Static Final intDef_div_scale = 10;//This class cannot be instantiated9 PrivateArith () {Ten }    One /**   A * provides accurate addition operations.  - * @paramv1 summand - * @paramv2 Addend the * @returntwo parameters of A and - */   -  Public Static DoubleAddDoubleV1,Doublev2) {    -BigDecimal B1 =NewBigDecimal (double.tostring (v1));  +BigDecimal B2 =NewBigDecimal (Double.tostring (v2));  - returnB1.add (B2). Doublevalue ();  + }    A /**   at * provides accurate subtraction operations.  - * @paramV1 is being subtracted - * @paramv2 meiosis - * @returnthe difference of two parameters - */   -  Public Static DoubleSubDoubleV1,Doublev2) {    inBigDecimal B1 =NewBigDecimal (double.tostring (v1));  -BigDecimal B2 =NewBigDecimal (Double.tostring (v2));  to returnb1.subtract (B2). Doublevalue ();  + }    - /**   the * provides accurate multiplication operations.  * * @paramV1 by multiplier $ * @paramV2 MultiplierPanax Notoginseng * @returnproduct of two parameters - */   the  Public Static DoubleMulDoubleV1,Doublev2) {    +BigDecimal B1 =NewBigDecimal (double.tostring (v1));  ABigDecimal B2 =NewBigDecimal (Double.tostring (v2));  the returnb1.multiply (B2). Doublevalue ();  + }    - /**   $ * Provide (relative) accurate division operation, when the occurrence of an endless situation, accurate to $ * After the decimal point 10 digits, the later numbers are rounded.  - * @paramV1 Dividend - * @paramv2 Divisor the * @returntwo parameters of the quotient - */  Wuyi  Public Static DoubleDivDoubleV1,Doublev2) {    the returnDiv (V1,v2,def_div_scale);  - }    Wu /**   - * Provide (relative) accurate division operations. When an exception occurs, the scale parameter refers to the About * Fixed precision, after which the numbers are rounded.  $ * @paramV1 Dividend - * @paramv2 Divisor - * @paramthe scale representation needs to be accurate to several points after the decimal point.  - * @returntwo parameters of the quotient A */   +  Public Static DoubleDivDoubleV1,DoubleV2,intScale ) {    the if(scale<0){    - Throw NewIllegalArgumentException ( $"The scale must is a positive integer or zero");  the }    theBigDecimal B1 =NewBigDecimal (double.tostring (v1));  theBigDecimal B2 =NewBigDecimal (Double.tostring (v2));  the returnb1.divide (b2,scale,bigdecimal.round_half_up). Doublevalue ();  - }    in /**   the * Provides precise rounding of decimal digits.  the * @paramv needs to be rounded up the number About * @paramafter the decimal point, keep several the * @returnresults after rounding the */   the  Public Static DoubleRoundDoubleVintScale ) {    + if(scale<0){    - Throw NewIllegalArgumentException ("The scale must is a positive integer or zero");  the }   BayiBigDecimal B =NewBigDecimal (double.tostring (v));  theBigDecimal one =NewBigDecimal ("1");  the returnb.divide (one,scale,bigdecimal.round_half_up). Doublevalue ();  - }    -};
    SoHow to convert BigDecimal to int or long
  • As follows:

    BigDecimal a=new BigDecimal (12.88); int B=a.intvalue (); System.out.println (b);//b=12;

      

Java uses the BigDecimal class to handle high-precision computations

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.