Introduction to BigDecimal class usage in Java

Source: Internet
Author: User

Java provides operation classes for large numbers (more than 16 significant bits), namely the Java.math.BinInteger class and the Java.math.BigDecimal class, for high-precision computations.
Where the BigInteger class is a processing class for large integers, and the BigDecimal class is a processing class for the size number.
The implementation of the BigDecimal class is used in the BigInteger class, and the difference is that the BigDecimal adds a decimal concept.
float and double can only be used for scientific calculations or engineering calculations; In business computing, the need for digital precision requires the use of the BigInteger class and the BigDecimal class, which supports the fixed-point number of any precision that can be used to accurately calculate currency values.
The BigDecimal class creates an object that cannot be mathematically calculated directly using the traditional + 、-、 *,/and arithmetic operators, but must call its corresponding method. The parameter of the method must also be an object of type BigDecimal.


The following methods are commonly used to construct BigDecimal objects:
BigDecimal BigDecimal (double D);
BigDecimal BigDecimal (String s);
Static BigDecimal valueOf (D1);


which
1. The method of constructing a double parameter is not allowed to use the!!!! Because it can not accurately get the corresponding value;
2. The String construction method is fully predictable: writing new BigDecimal ("0.1") will create a BigDecimal, which is exactly equal to the expected 0.1; Therefore, it is generally advisable to use the String construction method first;
3. Static method ValueOf (double val) is implemented internally, and the double type is still converted to String type; This is usually the preferred method of converting a double (or float) to BigDecimal;


The test code is as follows:

public static void Main (string[] args) {Double D1 = 0.10334;double D2 = 1234.0; System.out.println ("New BigDecimal (" +d1+ ") =" + new BigDecimal (D1) "); This approach is absolutely NOT allowed!!!!! System.out.println ("New BigDecimal (" +d2+ ") =" + New BigDecimal (D2) "); This approach is absolutely NOT allowed!!!!! System.out.println (""); System.out.println ("New BigDecimal (string.valueof (" +d1+ ")) =" + New BigDecimal (string.valueof (D1))); System.out.println ("New BigDecimal (string.valueof (" +d2+ ")) =" + New BigDecimal (string.valueof (D2))); System.out.println (""); System.out.println ("New BigDecimal (string.valueof (" +d1+ ")) =" + New BigDecimal (double.tostring (D1))); System.out.println ("New BigDecimal (string.valueof (" +d2+ ")) =" + New BigDecimal (Double.tostring (D2))); System.out.println (""); System.out.println ("Bigdecimal.valueof" ("+d1+") = "+ bigdecimal.valueof (D1)"); System.out.println ("Bigdecimal.valueof" ("+d2+") = "+ bigdecimal.valueof (D2)"); System.out.println (""); BigDecimal B1 = bigdecimal.valueof (1); BigDecimal b2 = bigdecimal.valueof (1.00000); System.out.println (B1.equals (B2)); System.out.println (B1.compareto (B2));}

The output is as follows:

New BigDecimal (0.10334) =0.10334000000000000130118138486068346537649631500244140625new BigDecimal (1234.0) =1234new BigDecimal (string.valueof (0.10334)) =0.10334new BigDecimal (string.valueof (1234.0)) =1234.0new BigDecimal ( String.valueof (0.10334)) =0.10334new BigDecimal (string.valueof (1234.0)) =1234.0bigdecimal.valueof (0.10334) = 0.10334bigdecimal.valueof (1234.0) =1234.0false0


Attached 1, BigDecimal class of ValueOf () method source code

public static BigDecimal ValueOf (double val) {return new BigDecimal (Double.tostring (Val));}

Attached 2, several common methods of BigDecimal class

/** * for remainder * The return value is (this% divisor) BigDecimal */bigdecimal remainder (BigDecimal divisor);/** * Seek the opposite number * The return value is (-this) BIGD Ecimal */bigdecimal negate ();/** * Compare this BigDecimal with a specified BigDecimal * Based on this method, two BigDecimal objects of equal value but with different scales (e.g., 2.0 and 2.00) are considered to be phase And so on; * Relative to six Boolean comparison operators (< = =,;, >=,! =, <=) Each method of each operator, giving precedence to this method; * The following statements are recommended for performing the above comparisons: (X.compareto (y) <op> 0), where <op> is one of six comparison operators; * * specified by: CompareTo in Interface comparable<bigdecimal> * return: When this BigDecimal is less than, equal to, or greater than Val in the number, returns-1, 0, or 1 */int compareTo (Big Decimal val);

Attached 3, provides accurate floating-point arithmetic (including add, subtract, multiply, divide, round) tool class source code

Package Com.util;import java.math.bigdecimal;/** * Provides accurate floating-point arithmetic (including add, subtract, multiply, divide, round) tool class */public class Arithutil {// Division operation default precision private static final int def_div_scale = 10;private Arithutil () {}/** * exact addition */public static double add (double val Ue1, double value2) {BigDecimal B1 = bigdecimal.valueof (value1); BigDecimal b2 = bigdecimal.valueof (value2); return B1.add (B2). Doublevalue (); /** * Exact subtraction */public static double sub (double value1, double value2) {BigDecimal B1 = bigdecimal.valueof (value1); BigDecimal b2 = bigdecimal.valueof (value2); return B1.subtract (B2). Doublevalue (); /** * Exact multiplication */public static double Mul (double value1, double value2) {BigDecimal B1 = bigdecimal.valueof (value1); BigDecimal b2 = bigdecimal.valueof (value2); return b1.multiply (B2). Doublevalue (); /** * Exact division uses the default precision */public static double div (double value1, double value2) throws Illegalaccessexception {return div (value 1, value2, Def_div_scale);} /** * Exact Division * @param scale Precision */public static double div (double value1, double value2, int SCALe) throws illegalaccessexception {if (Scale < 0) {throw new illegalaccessexception ("accuracy cannot be less than 0");} BigDecimal B1 = bigdecimal.valueof (value1); BigDecimal b2 = bigdecimal.valueof (value2);//Return B1.divide (B2, scale). Doublevalue (); return b1.divide (B2, scale, BIGDECIMAL.ROUND_HALF_UP). Doublevalue ();}  /** * Rounding * @param scale after decimal point retain several */public static double round (double v, int scale) throws Illegalaccessexception {return Div (V, 1, scale);} /** * Compare Size */public static Boolean Equalto (BigDecimal B1, BigDecimal B2) {if (B1 = = NULL | | b2 = NULL) {return false;} return 0 = = B1.compareto (b2);}}


Attached 4, another blog post can be consulted:

http://blog.csdn.net/jackiehff/article/details/8582449



Introduction to BigDecimal class usage in Java

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.