BigDecimal Class

Definition

Immutable, arbitrary-precision signed decimal numbers.

[Android.Runtime.Register("java/math/BigDecimal", DoNotGenerateAcw=true)]
public class BigDecimal : Java.Lang.Number, IDisposable, Java.Interop.IJavaPeerable, Java.Lang.IComparable
[<Android.Runtime.Register("java/math/BigDecimal", DoNotGenerateAcw=true)>]
type BigDecimal = class
    inherit Number
    interface ISerializable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
    interface IComparable
Inheritance
BigDecimal
Attributes
Implements

Remarks

Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer #unscaledValue() unscaled value and a 32-bit integer #scale() scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. The value of the number represented by the BigDecimal is therefore (unscaledValue &times; 10<sup>-scale</sup>).

The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The #toString method provides a canonical representation of a BigDecimal.

The BigDecimal class gives its user complete control over rounding behavior. If no rounding mode is specified and the exact result cannot be represented, an ArithmeticException is thrown; otherwise, calculations can be carried out to a chosen precision and rounding mode by supplying an appropriate MathContext object to the operation. In either case, eight <em>rounding modes</em> are provided for the control of rounding. Using the integer fields in this class (such as #ROUND_HALF_UP) to represent rounding mode is deprecated; the enumeration values of the RoundingModeenum, (such as RoundingMode#HALF_UP) should be used instead.

When a MathContext object is supplied with a precision setting of 0 (for example, MathContext#UNLIMITED), arithmetic operations are exact, as are the arithmetic methods which take no MathContext object. As a corollary of computing the exact result, the rounding mode setting of a MathContext object with a precision setting of 0 is not used and thus irrelevant. In the case of divide, the exact quotient could have an infinitely long decimal expansion; for example, 1 divided by 3. If the quotient has a nonterminating decimal expansion and the operation is specified to return an exact result, an ArithmeticException is thrown. Otherwise, the exact result of the division is returned, as done for other operations.

When the precision setting is not 0, the rules of BigDecimal arithmetic are broadly compatible with selected modes of operation of the arithmetic defined in ANSI X3.274-1996 and ANSI X3.274-1996/AM 1-2000 (section 7.4). Unlike those standards, BigDecimal includes many rounding modes. Any conflicts between these ANSI standards and the BigDecimal specification are resolved in favor of BigDecimal.

Since the same numerical value can have different representations (with different scales), the rules of arithmetic and rounding must specify both the numerical result and the scale used in the result's representation.

The different representations of the same numerical value are called members of the same cohort. The #compareTo(BigDecimal) natural order of BigDecimal considers members of the same cohort to be equal to each other. In contrast, the #equals(Object) equals method requires both the numerical value and representation to be the same for equality to hold. The results of methods like #scale() and #unscaledValue() will differ for numerically equal values with different representations.

In general the rounding modes and precision setting determine how operations return results with a limited number of digits when the exact result has more digits (perhaps infinitely many in the case of division and square root) than the number of digits returned.

First, the total number of digits to return is specified by the MathContext's precision setting; this determines the result's precision. The digit count starts from the leftmost nonzero digit of the exact result. The rounding mode determines how any discarded trailing digits affect the returned result.

For all arithmetic operators, the operation is carried out as though an exact intermediate result were first calculated and then rounded to the number of digits specified by the precision setting (if necessary), using the selected rounding mode. If the exact result is not returned, some digit positions of the exact result are discarded. When rounding increases the magnitude of the returned result, it is possible for a new digit position to be created by a carry propagating to a leading "9" digit. For example, rounding the value 999.9 to three digits rounding up would be numerically equal to one thousand, represented as 100&times;10<sup>1</sup>. In such cases, the new "1" is the leading digit position of the returned result.

For methods and constructors with a MathContext parameter, if the result is inexact but the rounding mode is RoundingMode#UNNECESSARY UNNECESSARY, an ArithmeticException will be thrown.

Besides a logical exact result, each arithmetic operation has a preferred scale for representing a result. The preferred scale for each operation is listed in the table below.

<table class="striped" style="text-align:left"> <caption>Preferred Scales for Results of Arithmetic Operations </caption> <thead> <tr><th scope="col">Operation</th><th scope="col">Preferred Scale of Result</th></tr> </thead> <tbody> <tr><th scope="row">Add</th><td>max(addend.scale(), augend.scale())</td> <tr><th scope="row">Subtract</th><td>max(minuend.scale(), subtrahend.scale())</td> <tr><th scope="row">Multiply</th><td>multiplier.scale() + multiplicand.scale()</td> <tr><th scope="row">Divide</th><td>dividend.scale() - divisor.scale()</td> <tr><th scope="row">Square root</th><td>radicand.scale()/2</td> </tbody> </table>

These scales are the ones used by the methods which return exact arithmetic results; except that an exact divide may have to use a larger scale since the exact result may have more digits. For example, 1/32 is 0.03125.

Before rounding, the scale of the logical exact intermediate result is the preferred scale for that operation. If the exact numerical result cannot be represented in precision digits, rounding selects the set of digits to return and the scale of the result is reduced from the scale of the intermediate result to the least scale which can represent the precision digits actually returned. If the exact result can be represented with at most precision digits, the representation of the result with the scale closest to the preferred scale is returned. In particular, an exactly representable quotient may be represented in fewer than precision digits by removing trailing zeros and decreasing the scale. For example, rounding to three digits using the RoundingMode#FLOOR floor rounding mode, <br>

19/100 = 0.19 // integer=19, scale=2<br>

but<br>

21/110 = 0.190 // integer=190, scale=3<br>

Note that for add, subtract, and multiply, the reduction in scale will equal the number of digit positions of the exact result which are discarded. If the rounding causes a carry propagation to create a new high-order digit position, an additional digit of the result is discarded than when no new digit position is created.

Other methods may have slightly different rounding semantics. For example, the result of the pow method using the #pow(int, MathContext) specified algorithm can occasionally differ from the rounded mathematical result by more than one unit in the last place, one #ulp() ulp.

Two types of operations are provided for manipulating the scale of a BigDecimal: scaling/rounding operations and decimal point motion operations. Scaling/rounding operations (#setScale setScale and #round round) return a BigDecimal whose value is approximately (or exactly) equal to that of the operand, but whose scale or precision is the specified value; that is, they increase or decrease the precision of the stored number with minimal effect on its value. Decimal point motion operations (#movePointLeft movePointLeft and #movePointRight movePointRight) return a BigDecimal created from the operand by moving the decimal point a specified distance in the specified direction.

As a 32-bit integer, the set of values for the scale is large, but bounded. If the scale of a result would exceed the range of a 32-bit integer, either by overflow or underflow, the operation may throw an ArithmeticException.

For the sake of brevity and clarity, pseudo-code is used throughout the descriptions of BigDecimal methods. The pseudo-code expression (i + j) is shorthand for "a BigDecimal whose value is that of the BigDecimali added to that of the BigDecimalj." The pseudo-code expression (i == j) is shorthand for "true if and only if the BigDecimali represents the same value as the BigDecimalj." Other pseudo-code expressions are interpreted similarly. Square brackets are used to represent the particular BigInteger and scale pair defining a BigDecimal value; for example [19, 2] is the BigDecimal numerically equal to 0.19 having a scale of 2.

All methods and constructors for this class throw NullPointerException when passed a null object reference for any input parameter.

Added in 1.1.

Java documentation for java.math.BigDecimal.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

BigDecimal(BigInteger)

Translates a BigInteger into a BigDecimal.

BigDecimal(BigInteger, Int32)

Translates a BigInteger unscaled value and an int scale into a BigDecimal.

BigDecimal(BigInteger, Int32, MathContext)

Translates a BigInteger unscaled value and an int scale into a BigDecimal, with rounding according to the context settings.

BigDecimal(BigInteger, MathContext)

Translates a BigInteger into a BigDecimal rounding according to the context settings.

BigDecimal(Char[])

Translates a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the #BigDecimal(String) constructor.

BigDecimal(Char[], Int32, Int32)

Translates a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the #BigDecimal(String) constructor, while allowing a sub-array to be specified.

BigDecimal(Char[], Int32, Int32, MathContext)

Translates a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the #BigDecimal(String) constructor, while allowing a sub-array to be specified and with rounding according to the context settings.

BigDecimal(Char[], MathContext)

Translates a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the #BigDecimal(String) constructor and with rounding according to the context settings.

BigDecimal(Double)

Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value.

BigDecimal(Double, MathContext)

Translates a double into a BigDecimal, with rounding according to the context settings.

BigDecimal(Int32)

Translates an int into a BigDecimal.

BigDecimal(Int32, MathContext)

Translates an int into a BigDecimal, with rounding according to the context settings.

BigDecimal(Int64)

Translates a long into a BigDecimal.

BigDecimal(Int64, MathContext)

Translates a long into a BigDecimal, with rounding according to the context settings.

BigDecimal(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

BigDecimal(String)

Translates the string representation of a BigDecimal into a BigDecimal.

BigDecimal(String, MathContext)

Translates the string representation of a BigDecimal into a BigDecimal, accepting the same strings as the #BigDecimal(String) constructor, with rounding according to the context settings.

Fields

RoundCeiling

Rounding mode to round towards positive infinity.

RoundDown

Rounding mode to round towards zero.

RoundFloor

Rounding mode to round towards negative infinity.

RoundHalfDown

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.

RoundHalfEven

Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.

RoundHalfUp

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.

RoundUnnecessary

Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.

RoundUp

Rounding mode to round away from zero.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
One

The value 1, with a scale of 0.

PeerReference (Inherited from Object)
Ten

The value 10, with a scale of 0.

ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Zero

The value 0, with a scale of 0.

Methods

Abs()

Returns a BigDecimal whose value is the absolute value of this BigDecimal, and whose scale is this.scale().

Abs(MathContext)

Returns a BigDecimal whose value is the absolute value of this BigDecimal, with rounding according to the context settings.

Add(BigDecimal)

Returns a BigDecimal whose value is (this + augend), and whose scale is max(this.scale(), augend.scale()).

Add(BigDecimal, MathContext)

Returns a BigDecimal whose value is (this + augend), with rounding according to the context settings.

ByteValue()

Returns the value of the specified number as a byte.

(Inherited from Number)
ByteValueExact()

Converts this BigDecimal to a byte, checking for lost information.

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
CompareTo(BigDecimal)

Compares this BigDecimal with the specified BigDecimal.

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Divide(BigDecimal)

Returns a BigDecimal whose value is (this / divisor), and whose preferred scale is (this.scale() - divisor.scale()); if the exact quotient cannot be represented (because it has a non-terminating decimal expansion) an ArithmeticException is thrown.

Divide(BigDecimal, Int32, RoundingMode)

Returns a BigDecimal whose value is (this / divisor), and whose scale is as specified.

Divide(BigDecimal, Int32, RoundOptions)

Returns a BigDecimal whose value is (this / divisor), and whose scale is as specified.

Divide(BigDecimal, MathContext)

Returns a BigDecimal whose value is (this / divisor), with rounding according to the context settings.

Divide(BigDecimal, RoundingMode)

Returns a BigDecimal whose value is (this / divisor), and whose scale is this.scale().

Divide(BigDecimal, RoundOptions)

Returns a BigDecimal whose value is (this / divisor), and whose scale is this.scale().

DivideAndRemainder(BigDecimal)

Returns a two-element BigDecimal array containing the result of divideToIntegralValue followed by the result of remainder on the two operands.

DivideAndRemainder(BigDecimal, MathContext)

Returns a two-element BigDecimal array containing the result of divideToIntegralValue followed by the result of remainder on the two operands calculated with rounding according to the context settings.

DivideToIntegralValue(BigDecimal)

Returns a BigDecimal whose value is the integer part of the quotient (this / divisor) rounded down.

DivideToIntegralValue(BigDecimal, MathContext)

Returns a BigDecimal whose value is the integer part of (this / divisor).

DoubleValue()

Converts this BigDecimal to a double.

Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
FloatValue()

Converts this BigDecimal to a float.

GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
IntValue()

Converts this BigDecimal to an int.

IntValueExact()

Converts this BigDecimal to an int, checking for lost information.

JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
LongValue()

Converts this BigDecimal to a long.

LongValueExact()

Converts this BigDecimal to a long, checking for lost information.

Max(BigDecimal)

Returns the maximum of this BigDecimal and val.

Min(BigDecimal)

Returns the minimum of this BigDecimal and val.

MovePointLeft(Int32)

Returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the left.

MovePointRight(Int32)

Returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the right.

Multiply(BigDecimal)

Returns a BigDecimal whose value is (this &times; multiplicand), and whose scale is (this.scale() + multiplicand.scale()).

Multiply(BigDecimal, MathContext)

Returns a BigDecimal whose value is (this &times; multiplicand), with rounding according to the context settings.

Negate()

Returns a BigDecimal whose value is (-this), and whose scale is this.scale().

Negate(MathContext)

Returns a BigDecimal whose value is (-this), with rounding according to the context settings.

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
Plus()

Returns a BigDecimal whose value is (+this), and whose scale is this.scale().

Plus(MathContext)

Returns a BigDecimal whose value is (+this), with rounding according to the context settings.

Pow(Int32)

Returns a BigDecimal whose value is (this<sup>n</sup>), The power is computed exactly, to unlimited precision.

Pow(Int32, MathContext)

Returns a BigDecimal whose value is (this<sup>n</sup>).

Precision()

Returns the precision of this BigDecimal.

Remainder(BigDecimal)

Returns a BigDecimal whose value is (this % divisor).

Remainder(BigDecimal, MathContext)

Returns a BigDecimal whose value is (this % divisor), with rounding according to the context settings.

Round(MathContext)

Returns a BigDecimal rounded according to the MathContext settings.

Scale()

Returns the scale of this BigDecimal.

ScaleByPowerOfTen(Int32)

Returns a BigDecimal whose numerical value is equal to (this * 10<sup>n</sup>).

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
SetScale(Int32)

Returns a BigDecimal whose scale is the specified value, and whose value is numerically equal to this BigDecimal's.

SetScale(Int32, RoundingMode)

Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.

SetScale(Int32, RoundOptions)

Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.

ShortValue()

Returns the value of the specified number as a short.

(Inherited from Number)
ShortValueExact()

Converts this BigDecimal to a short, checking for lost information.

Signum()

Returns the signum function of this BigDecimal.

Sqrt(MathContext)

Returns an approximation to the square root of this with rounding according to the context settings.

StripTrailingZeros()

Returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation.

Subtract(BigDecimal)

Returns a BigDecimal whose value is (this - subtrahend), and whose scale is max(this.scale(), subtrahend.scale()).

Subtract(BigDecimal, MathContext)

Returns a BigDecimal whose value is (this - subtrahend), with rounding according to the context settings.

ToArray<T>() (Inherited from Object)
ToBigInteger()

Converts this BigDecimal to a BigInteger.

ToBigIntegerExact()

Converts this BigDecimal to a BigInteger, checking for lost information.

ToEngineeringString()

Returns a string representation of this BigDecimal, using engineering notation if an exponent is needed.

ToPlainString()

Returns a string representation of this BigDecimal without an exponent field.

ToString()

Returns a string representation of the object.

(Inherited from Object)
Ulp()

Returns the size of an ulp, a unit in the last place, of this BigDecimal.

UnregisterFromRuntime() (Inherited from Object)
UnscaledValue()

Returns a BigInteger whose value is the unscaled value of this BigDecimal.

ValueOf(Double)

Translates a double into a BigDecimal, using the double's canonical string representation provided by the Double#toString(double) method.

ValueOf(Int64)

Translates a long value into a BigDecimal with a scale of zero.

ValueOf(Int64, Int32)

Translates a long unscaled value and an int scale into a BigDecimal.

Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IComparable.CompareTo(Object)
IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to