BigDecimal Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
The BigDecimal
class implements immutable arbitrary-precision decimal numbers.
[Android.Runtime.Register("android/icu/math/BigDecimal", ApiSince=24, DoNotGenerateAcw=true)]
public class BigDecimal : Java.Lang.Number, IDisposable, Java.Interop.IJavaPeerable, Java.Lang.IComparable
[<Android.Runtime.Register("android/icu/math/BigDecimal", ApiSince=24, DoNotGenerateAcw=true)>]
type BigDecimal = class
inherit Number
interface ISerializable
interface IJavaObject
interface IDisposable
interface IJavaPeerable
interface IComparable
- Inheritance
- Attributes
- Implements
Remarks
The BigDecimal
class implements immutable arbitrary-precision decimal numbers. The methods of the BigDecimal
class provide operations for fixed and floating point arithmetic, comparison, format conversions, and hashing.
As the numbers are decimal, there is an exact correspondence between an instance of a BigDecimal
object and its String
representation; the BigDecimal
class provides direct conversions to and from String
and character array (char[]
) objects, as well as conversions to and from the Java primitive types (which may not be exact) and BigInteger
.
In the descriptions of constructors and methods in this documentation, the value of a BigDecimal
number object is shown as the result of invoking the toString()
method on the object. The internal representation of a decimal number is neither defined nor exposed, and is not permitted to affect the result of any operation.
The floating point arithmetic provided by this class is defined by the ANSI X3.274-1996 standard, and is also documented at http://www2.hursley.ibm.com/decimal
<br> [This URL will change.]<h3>Operator methods</h3>
Operations on BigDecimal
numbers are controlled by a MathContext
object, which provides the context (precision and other information) for the operation. Methods that can take a MathContext
parameter implement the standard arithmetic operators for BigDecimal
objects and are known as operator methods. The default settings provided by the constant MathContext#DEFAULT
(digits=9, form=SCIENTIFIC, lostDigits=false, roundingMode=ROUND_HALF_UP
) perform general-purpose floating point arithmetic to nine digits of precision. The MathContext
parameter must not be null
.
Each operator method also has a version provided which does not take a MathContext
parameter. For this version of each method, the context settings used are digits=0, form=PLAIN, lostDigits=false, roundingMode=ROUND_HALF_UP
; these settings perform fixed point arithmetic with unlimited precision, as defined for the original BigDecimal class in Java 1.1 and Java 1.2.
For monadic operators, only the optional MathContext
parameter is present; the operation acts upon the current object.
For dyadic operators, a BigDecimal
parameter is always present; it must not be null
. The operation acts with the current object being the left-hand operand and the BigDecimal
parameter being the right-hand operand.
For example, adding two BigDecimal
objects referred to by the names award
and extra
could be written as any of:
award.add(extra) <br>award.add(extra, MathContext.DEFAULT) <br>award.add(extra, acontext)
(where acontext
is a MathContext
object), which would return a BigDecimal
object whose value is the result of adding award
and extra
under the appropriate context settings.
When a BigDecimal
operator method is used, a set of rules define what the result will be (and, by implication, how the result would be represented as a character string). These rules are defined in the BigDecimal arithmetic documentation (see the URL above), but in summary: <ul> <li>Results are normally calculated with up to some maximum number of significant digits. For example, if the MathContext
parameter for an operation were MathContext.DEFAULT
then the result would be rounded to 9 digits; the division of 2 by 3 would then result in 0.666666667. <br> You can change the default of 9 significant digits by providing the method with a suitable MathContext
object. This lets you calculate using as many digits as you need -- thousands, if necessary. Fixed point (scaled) arithmetic is indicated by using a digits
setting of 0 (or omitting the MathContext
parameter). <br> Similarly, you can change the algorithm used for rounding from the default "classic" algorithm. <li> In standard arithmetic (that is, when the form
setting is not PLAIN
), a zero result is always expressed as the single digit '0'
(that is, with no sign, decimal point, or exponent part). <li> Except for the division and power operators in standard arithmetic, trailing zeros are preserved (this is in contrast to binary floating point operations and most electronic calculators, which lose the information about trailing zeros in the fractional part of results). <br> So, for example:
new BigDecimal("2.40").add( new BigDecimal("2")) => "4.40" <br>new BigDecimal("2.40").subtract(new BigDecimal("2")) => "0.40" <br>new BigDecimal("2.40").multiply(new BigDecimal("2")) => "4.80" <br>new BigDecimal("2.40").divide( new BigDecimal("2"), def) => "1.2"
where the value on the right of the =>
would be the result of the operation, expressed as a String
, and def
(in this and following examples) refers to MathContext.DEFAULT
). This preservation of trailing zeros is desirable for most calculations (including financial calculations). If necessary, trailing zeros may be easily removed using division by 1. <li> In standard arithmetic, exponential form is used for a result depending on its value and the current setting of digits
(the default is 9 digits). If the number of places needed before the decimal point exceeds the digits
setting, or the absolute value of the number is less than 0.000001
, then the number will be expressed in exponential notation; thus
new BigDecimal("1e+6").multiply(new BigDecimal("1e+6"), def)
results in 1E+12
instead of 1000000000000
, and
new BigDecimal("1").divide(new BigDecimal("3E+10"), def)
results in 3.33333333E-11
instead of 0.0000000000333333333
.
The form of the exponential notation (scientific or engineering) is determined by the form
setting. </ul>
The names of methods in this class follow the conventions established by java.lang.Number
, java.math.BigInteger
, and java.math.BigDecimal
in Java 1.1 and Java 1.2.
Java documentation for android.icu.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(BigDecimal) |
Constructs a |
BigDecimal(BigInteger, Int32) |
Constructs a |
BigDecimal(BigInteger) |
Constructs a |
BigDecimal(Char[], Int32, Int32) |
Constructs a |
BigDecimal(Char[]) |
Constructs a |
BigDecimal(Double) |
Constructs a |
BigDecimal(Int32) |
Constructs a |
BigDecimal(Int64) |
Constructs a |
BigDecimal(IntPtr, JniHandleOwnership) | |
BigDecimal(String) |
Constructs a |
Fields
RoundCeiling |
Obsolete.
Rounding mode to round to a more positive number. |
RoundDown |
Obsolete.
Rounding mode to round towards zero. |
RoundFloor |
Obsolete.
Rounding mode to round to a more negative number. |
RoundHalfDown |
Obsolete.
Rounding mode to round to nearest neighbor, where an equidistant value is rounded down. |
RoundHalfEven |
Obsolete.
Rounding mode to round to nearest neighbor, where an equidistant value is rounded to the nearest even neighbor. |
RoundHalfUp |
Obsolete.
Rounding mode to round to nearest neighbor, where an equidistant value is rounded up. |
RoundUnnecessary |
Obsolete.
Rounding mode to assert that no rounding is necessary. |
RoundUp |
Obsolete.
Rounding mode to round away from zero. |
Properties
Class |
Returns the runtime class of this |
Handle |
The handle to the underlying Android instance. (Inherited from Object) |
JniIdentityHashCode | (Inherited from Object) |
JniPeerMembers | |
One |
The |
PeerReference | (Inherited from Object) |
Ten |
The |
ThresholdClass | |
ThresholdType | |
Zero |
The |
Methods
Abs() |
Returns a plain |
Abs(MathContext) |
Returns a |
Add(BigDecimal, MathContext) |
Returns a |
Add(BigDecimal) |
Returns a plain |
ByteValue() |
Returns the value of the specified number as a |
ByteValueExact() |
Converts this |
Clone() |
Creates and returns a copy of this object. (Inherited from Object) |
CompareTo(BigDecimal, MathContext) |
Compares this |
CompareTo(BigDecimal) |
Compares this |
Dispose() | (Inherited from Object) |
Dispose(Boolean) | (Inherited from Object) |
Divide(BigDecimal, Int32, RoundOptions) |
Returns a plain |
Divide(BigDecimal, MathContext) |
Returns a |
Divide(BigDecimal, RoundOptions) |
Returns a plain |
Divide(BigDecimal) |
Returns a plain |
DivideInteger(BigDecimal, MathContext) |
Returns a |
DivideInteger(BigDecimal) |
Returns a plain |
DoubleValue() |
Converts this |
Equals(Object) |
Indicates whether some other object is "equal to" this one. (Inherited from Object) |
FloatValue() |
Converts this |
Format(Int32, Int32, Int32, Int32, Int32, RoundOptions) |
Returns the |
Format(Int32, Int32) |
Returns the |
GetHashCode() |
Returns a hash code value for the object. (Inherited from Object) |
IntValue() |
Converts this |
IntValueExact() |
Converts this |
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 |
LongValueExact() |
Converts this |
Max(BigDecimal, MathContext) |
Returns a |
Max(BigDecimal) |
Returns a plain |
Min(BigDecimal, MathContext) |
Returns a |
Min(BigDecimal) |
Returns a plain |
MovePointLeft(Int32) |
Returns a plain |
MovePointRight(Int32) |
Returns a plain |
Multiply(BigDecimal, MathContext) |
Returns a |
Multiply(BigDecimal) |
Returns a plain |
Negate() |
Returns a plain |
Negate(MathContext) |
Returns a |
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 plain |
Plus(MathContext) |
Returns a |
Pow(BigDecimal, MathContext) |
Returns a |
Pow(BigDecimal) |
Returns a plain |
Remainder(BigDecimal, MathContext) |
Returns a |
Remainder(BigDecimal) |
Returns a plain |
Scale() |
Returns the scale of this |
SetHandle(IntPtr, JniHandleOwnership) |
Sets the Handle property. (Inherited from Object) |
SetScale(Int32, RoundOptions) |
Returns a plain |
SetScale(Int32) |
Returns a plain |
ShortValue() |
Returns the value of the specified number as a |
ShortValueExact() |
Converts this |
Signum() |
Returns the sign of this |
Subtract(BigDecimal, MathContext) |
Returns a |
Subtract(BigDecimal) |
Returns a plain |
ToArray<T>() | (Inherited from Object) |
ToBigDecimal() |
Converts this |
ToBigInteger() |
Converts this |
ToBigIntegerExact() |
Converts this |
ToCharArray() |
Returns the |
ToString() |
Returns a string representation of the object. (Inherited from Object) |
UnregisterFromRuntime() | (Inherited from Object) |
UnscaledValue() |
Returns the number as a |
ValueOf(Double) |
Translates a |
ValueOf(Int64, Int32) |
Translates a |
ValueOf(Int64) |
Translates a |
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, 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) |
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) |
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) |
Gets the JNI name of the type of the instance |
JavaAs<TResult>(IJavaPeerable) |
Try to coerce |
TryJavaCast<TResult>(IJavaPeerable, TResult) |
Try to coerce |