Java BigDecimal Detailed, provides rich rounding rules

Source: Internet
Author: User
Tags comparable pow

The Java.math.BigDecimal class provides arithmetic, scale operations, rounding, comparison, hashing algorithms, and format conversion operations.

The ToString () method provides a canonical representation of the BigDecimal. It gives the user full control over the rounding behavior.

Provides two types of operations for manipulating BigDecimal scale:

    • Scaling/Rounding Operations
    • The decimal move operation.

This class and its iterators implement all the optional methods of the comparable interface.

class declaration

?
12 publicclass BigDecimal extends Number implementsComparable<bigdecimal></bigdecimal>
Field fields

The following are the fields in the Java.math.BigDecimal class:

    • The static BigDecimal one--value is 1, with a scale of 0.

    • The static BigDecimal ten--value is 10, with a scale of 0.

    • The static BigDecimal zero--value is 0, with a scale of 0.

    • The static int round_ceiling--rounding mode is inward positive infinity.

    • The static int round_down--rounding mode is rounded to 0.

    • The static int round_floor--rounding mode is close to negative infinity.

    • The static int round_half_down--rounding mode is rounded to nearest neighbor if the distance to the two adjacent numbers is equal, in this case, rounding down.

    • static int round_half_even--rounding mode to "nearest neighbor" if the distance to two adjacent numbers is equal, in this case, rounding toward more adjacent.

    • The static int round_half_up--rounding mode is rounded to "nearest neighbor" if the distance is equal to two adjacent digits, in this case the range.

    • The static int round_unnecessary--rounding mode asserts that the requested operation has precise results and therefore does not require rounding.

    • The static int round_up--rounding mode rounds to zero.

Class constructors
S.N. Constructors & Descriptions
1 BigDecimal (BigInteger val)
This constructor is used to convert BigInteger to BigDecimal.
2 BigDecimal (BigInteger unscaledval, int scale)
This constructor is used to convert to BigInteger non-scale values and an int scale into a bigdecimal.
3 BigDecimal (BigInteger unscaledval, int scale, Mathcontext MC)
This constructor is used to convert to BigInteger non-scale values and an int scale to BigDecimal, which is rounded based on context settings.
4 BigDecimal (BigInteger val, Mathcontext MC)
This constructor is used to convert BigInteger to BigDecimal rounding based on context settings.
5 BigDecimal (char[] in)
This constructor is used to convert the BigDecimal character array representation to BigDecimal, accepting the same character sequence as the BigDecimal (String) constructor.
6 BigDecimal (char[] in, int offset, int len)
This constructor is used to convert the BigDecimal character array representation to BigDecimal, which accepts the same character sequence as the BigDecimal (String) construction method, while allowing the specified subarray.
7 BigDecimal (char[] in, int offset, int len, Mathcontext MC)
This constructor is used to convert the BigDecimal character array representation to BigDecimal, which accepts a character sequence that is the same as the BigDecimal (String) constructor, allowing the specified sub-array and rounding with the context setting.
8 BigDecimal (char[] in, Mathcontext MC)
This constructor is used to convert the BigDecimal character array representation to BigDecimal, accepting the same sequence of characters as the BigDecimal (String) construct and rounding based on the context settings.
9 BigDecimal (double val)
This constructor is used to convert a double to an exact decimal representation of a bigdecimal, which is a binary floating-point value of double.
10 BigDecimal (double Val, Mathcontext MC)
This constructor is used to convert a double to a bigdecimal, which has to be rounded based on the context setting.
11 BigDecimal (int val)
This constructor is used to convert an int to BigDecimal.
12 BigDecimal (int val, Mathcontext MC)
This constructor is used to convert an int to BigDecimal, which has to be rounded based on the context setting.
13 BigDecimal (Long Val)
This constructor is used to convert long to a bigdecimal.
14 BigDecimal (Long Val, Mathcontext MC)
This constructor converts BigInteger to BigDecimal.
15 BigDecimal (String val)
This constructor is used to convert the string representation of a bigdecimal to BigDecimal.
16 BigDecimal (String val, Mathcontext MC)
This constructor is used to convert the string representation of BigDecimal to BigDecimal, accepting the same string as the BigDecimal (string) construct, and rounding based on the context settings.

Class method

S.N. Method & Description
1 BigDecimal ABS ()
This method returns a BigDecimal whose value is the absolute number of this bigdecimal, with a scale of This.scale ().
2 BigDecimal ABS (Mathcontext MC)
This method returns a BigDecimal whose value is the absolute of this bigdecimal, and is rounded against the context setting.
3 BigDecimal Add (BigDecimal augend)
This method returns a BigDecimal with a value of (this + augend) with a scale of Max (This.scale (), Augend.scale ()).
4 BigDecimal Add (BigDecimal augend, Mathcontext MC)
This method returns a BigDecimal with a value of (this + Augend), which is rounded against the context setting.
5 Byte Bytevalueexact ()
The bigdecimal of this method is converted to a byte to check for lost information.
6 int CompareTo (BigDecimal val)
This method compares bigdecimal with the specified BigDecimal.
7 BigDecimal pide (BigDecimal pisor)
This method returns a BigDecimal with a value of (this/divisor), and its preferred scale is (This.scale ()-Pisor.scale ()), if the exact quotient cannot be represented (because it has an infinite decimal extension), The ArithmeticException is thrown.
8 BigDecimal pide (BigDecimal pisor, int roundingmode)
This method returns a BigDecimal with a value of (this/divisor) with a scale of This.scale ().
9 BigDecimal pide (BigDecimal pisor, int scale, int roundingmode)
This method returns a BigDecimal whose value is (this/divisor), as specified by its scale.
10 BigDecimal pide (BigDecimal pisor, int scale, Roundingmode roundingmode)
This method returns a BigDecimal with a value of (this/divisor) whose scale is specified.
11 BigDecimal pide (BigDecimal pisor, Mathcontext MC)
This method returns a BigDecimal with a value of (this/divisor), which is rounded against the context setting.
12 BigDecimal pide (BigDecimal pisor, Roundingmode roundingmode)
This method returns a BigDecimal with a value of (this/divisor) with a scale of This.scale ().
13 Bigdecimal[] Pideandremainder (BigDecimal pisor)
This method returns a BigDecimal array that contains pidetointegralvalue results, followed by the result of the remaining two operands, consisting of two elements.
14 Bigdecimal[] Pideandremainder (BigDecimal pisor, Mathcontext MC)
This method returns a result that contains Pidetointegralvalue, followed by the rest of the BigDecimal array with the result of rounding the results of two operands based on the context setting, consisting of two elements.
15 BigDecimal Pidetointegralvalue (BigDecimal pisor)
This method returns a BigDecimal whose value is the integer part of the quotient (this/divisor) is rounded.
16 BigDecimal Pidetointegralvalue (BigDecimal pisor, Mathcontext MC)
This method returns a BigDecimal whose value is the integer portion of (this/divisor).
17 Double Doublevalue ()
This method converts bigdecimal to double.
18 Boolean Equals (Object x)
This method compares the BigDecimal with the specified object for equality.
19 Float Floatvalue ()
This method converts the BigDecimal to float.
20 int Hashcode ()
This method returns the hash code for the BigDecimal.
21st int Intvalue ()
This method converts bigdecimal to int.
22 int Intvalueexact ()
This method converts bigdecimal to int and checks for missing information.
23 Long Longvalue ()
This method converts bigdecimal to long.
24 Long Longvalueexact ()
This method converts bigdecimal to long and checks for missing information.
25 BigDecimal Max (BigDecimal val)
This method returns the maximum value for this BigDecimal and Val.
26 BigDecimal min (BigDecimal val)
This method returns the minimum value for this bigdecimal and Val.
27 BigDecimal movepointleft (int n)
This method returns a BigDecimal, which is equivalent to moving the decimal point of the value N to the left.
28 BigDecimal movepointright (int n)
This method returns a BigDecimal, which is equivalent to moving the decimal point of the value N to the right.
29 BigDecimal Multiply (BigDecimal multiplicand)
This method returns a BigDecimal with a value of (Thisx by multiplier) with a scale of (This.scale () + Multiplicand.scale ()).
30 BigDecimal Multiply (BigDecimal multiplicand, Mathcontext MC)
This method returns a BigDecimal with a value of (Thisx multiplier) to be rounded based on the context setting.
31 BigDecimal negate ()
This method returns a BigDecimal whose value is (+this), with a scale of This.scale ().
32 BigDecimal negate (Mathcontext MC)
This method returns a BigDecimal whose value is (-this), rounded according to the context setting.
33 BigDecimal Plus ()
This method returns a BigDecimal whose value is (+this), with a scale of This.scale ().
34 BigDecimal Plus (Mathcontext MC)
This method returns a BigDecimal whose value is (+this), rounded according to the context setting.
35 BigDecimal pow (int n)
This method returns a BigDecimal whose value is (THISN) and the power is computed precisely to make it infinitely accurate.
36 BigDecimal Pow (int n, Mathcontext MC)
This method returns a BigDecimal whose value is (THISN).
37 int precision ()
This method returns the precision of this bigdecimal.
38 BigDecimal remainder (BigDecimal pisor)
This method converts bigdecimal to a byte, checking for missing information.
39 BigDecimal remainder (BigDecimal pisor, Mathcontext MC)
This method returns a BigDecimal whose value is (this% divisor), rounded according to the context setting.
40 BigDecimal round (Mathcontext MC)
This method returns a bigdecimal that is rounded according to the Mathcontext setting.
41 int scale ()
This method returns the scale of this bigdecimal.
42 BigDecimal Scalebypoweroften (int n)
This method returns a bigdecimal whose value is equal to (this * 10n).
43 BigDecimal setscale (int newscale)
This method returns a bigdecimal whose scale is the specified value and whose value is equal to the BigDecimal in number.
44 BigDecimal setscale (int newscale, int roundingmode)
This method returns a bigdecimal whose scale is the specified value, and its non-scale value is multiplied by or divided by the non-scale value of this bigdecimal by the power of 10, to maintain its overall value.
45 BigDecimal setscale (int newscale, Roundingmode roundingmode)
This method returns a bigdecimal whose scale is the specified value, and its non-scale value is multiplied by or divided by the non-scale value of this bigdecimal by the power of 10, to maintain its overall price decision.
46 Short Shortvalueexact ()
This method converts the BigDecimal to short and examines the missing information.
47 int Signum ()
This method returns the Sign function for this bigdecimal.
48 BigDecimal Striptrailingzeros ()
This method returns a bigdecimal that is equal to this one on a numeric value, but with all trailing zeros removed from the representation.
49 BigDecimal Subtract (BigDecimal subtrahend)
This method returns a BigDecimal with a value of (this-meiosis) with the scale of Max (This.scale (), Subtrahend.scale ()).
50 BigDecimal Subtract (BigDecimal subtrahend, Mathcontext MC)
This method returns a BigDecimal whose value is (this-meiosis), and is rounded based on the context setting.
51 BigInteger Tobiginteger ()
This method converts bigdecimal to BigInteger.
52 BigInteger Tobigintegerexact ()
This method converts bigdecimal to BigInteger and checks for missing information.
53 String toengineeringstring ()
This method returns the string representation of this bigdecimal, using the engineering notation, if an exponent is required.
54 String toplainstring ()
This method returns the string representation of this bigdecimal without a digit field.
55 String toString ()
This method returns the string representation of this bigdecimal, using scientific notation if an exponent is required.
56 BigDecimal ULP ()
This method returns a ULP of the size of this bigdecimal, in the last unit.
57 BigInteger Unscaledvalue ()
This method returns a BigInteger whose value is the non-scale value of this bigdecimal.
58 Static BigDecimal ValueOf (double val)
This method converts a double to a bigdecimal, using the normalized string representation of a double provided by the double.tostring (double) method.
59 Static BigDecimal ValueOf (long Val)
This method converts a long value to a value of 0 with a scale of bigdecimal.
60 Static BigDecimal valueOf (long unscaledval, int scale)
This method converts the non-scale value of long and an int scale into a bigdecimal.

Acknowledgement: https://www.2cto.com/kf/201608/533725.html

Java BigDecimal Detailed, provides rich rounding rules

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.