Decimal class

Decimal is a fast implementation of decimal numbers, using an internal DynamicInt to represent them with a fixed precision.

In theory a Decimal can represent any decimal number, where the whole part can be represented as int or BigInt and the precision is from 0 to 15.

  • All the operations will try to preserve results precision and avoid overflows, by dynamically changing the precision of the internal representation.

  • This implementation does not use fractions for its operations, and depends on the maximum bits that a DynamicInt can represent. See maxSafeInteger.

  • Note that double (floating-points) are not precise in the representation of decimal numbers. For ex.: 0.2 + 0.1 = 0.30000000000000004

Implemented types
Available Extensions

Constructors

Decimal.fromBigInt(BigInt n, {int precision = 0})
Creates a Decimal from a BigInt.
factory
Decimal.fromDouble(double d, {int? precision})
Creates a Decimal from a double.
factory
Decimal.fromDynamicInt(DynamicInt n, {int precision = 0})
Creates a Decimal from a DynamicInt.
factory
Decimal.fromInt(int n, {int precision = 0})
Creates a Decimal from an int.
factory
Decimal.fromNum(num n, {int? precision})
Creates a Decimal from a num.
factory
Decimal.fromParts(Object whole, Object decimal, {int? precision})
factory
Decimal.parse(String s, {int? precision, String decimalDelimiter = '.'})
Parses a Decimal from s.
factory
Decimal.parseParts(String whole, String decimal, {int? precision})
factory

Properties

compactedPrecision Decimal
Compacts this Decimal to the minimal precision needed to represent this decimal.
no setter
decimalPartAsDouble double
Returns the decimal part of this number as double.
no setter
decimalPartAsString String
Returns the decimal part of this number as String.
no setter
hashCode int
The hash code for this object.
no setteroverride
isBigInt bool
Returns true if the internal representation is a BigInt.
no setteroverride
isDecimal bool
Returns true if this instance is a Decimal
no setteroverride
isDecimalPartZero bool
Returns true if the decimal part is zero (an integer number).
no setter
isDynamicInt bool
Returns true if this instance is a DynamicInt
no setteroverride
isEven bool
Returns true if and only if this Decimal isWholeNumber and is even.
no setteroverride
isNegative bool
Returns true if this number is negative.
no setteroverride
isOdd bool
Returns true if and only if this Decimal isWholeNumber and is odd.
no setteroverride
isOne bool
Returns true if this number is one.
no setteroverride
isPositive bool
Returns true if this number is positive.
no setteroverride
isSafeInteger bool
Returns true if this number is safe to be represented as int for the current Dart platform.
no setteroverride
isWholeNumber bool
Returns true if this is a whole number.
no setteroverride
isZero bool
Returns true if this number is zero.
no setteroverride
precision int
The precision of the internal representation.
final
precisionNeeded int
Returns the needed precision to represent this decimal.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
sign int
Returns the sign of this number. See int.sign.
no setteroverride
square Decimal
The square of this number.
no setteroverride
squareRoot Decimal
The square root of this number.
no setteroverride
wholePart DynamicInt
The whole part of this decimal.
no setter
wholePartAsBigInt BigInt
wholePart as BigInt.
no setter
wholePartAsDouble double
The whole part of this decimal as double.
no setter
wholePartAsInt int
wholePart as int.
no setter
wholePartAsString String
The whole part of this decimal as String.
no setter
wholePartDigits int
Returns the number of digits ot represent this decimal whole part.
no setter

Methods

abs() Decimal
override
clipPrecision(int minimumPrecision, int maximumPrecision) Decimal
Clips the precision in range minimumPrecision .. maximumPrecision (inclusive). Returns withPrecision if needed.
compareTo(DynamicNumber other) int
Compares this object to another object.
override
divide(num n2) Decimal
override
divideBigInt(BigInt n2) Decimal
override
divideBigIntAsDecimal(BigInt n2) Decimal
override
divideBigIntAsDouble(BigInt n2) double
override
divideDouble(double n2) Decimal
override
divideDoubleAsDecimal(double n2) Decimal
override
divideDoubleAsDouble(double n2) double
override
divideDynamicInt(DynamicInt other) Decimal
override
divideDynamicIntAsDecimal(DynamicInt other) Decimal
override
divideDynamicIntAsDouble(DynamicInt n2) double
override
divideDynamicIntAsDynamicInt(DynamicInt other) DynamicInt
override
divideInt(int n2) Decimal
override
divideIntAsDecimal(int n2) Decimal
override
divideIntAsDouble(int n2) double
override
divideNumAsDecimal(num n2) Decimal
override
divideNumAsDouble(num n2) double
override
equalsBigInt(BigInt n) bool
Returns true if n is equals to this number.
override
equalsDynamicInt(DynamicInt other) bool
Returns true if other is equals to this number.
override
equalsInt(int n) bool
Returns true if n is equals to this number.
override
format({bool compact = false, String decimalDelimiter = '.', bool thousands = true, String thousandsDelimiter = ',', bool decimal = true}) String
Formats this decimal to a String.
override
higherPrecision(Decimal other) int
Returns the higher precision between this and other.
lowerPrecision(Decimal other) int
Returns the lower precision between this and other.
moduloBigInt(BigInt n2) Decimal
Euclidean modulo for BigInt n2.
override
moduloDynamicInt(DynamicInt other) Decimal
Euclidean modulo for DynamicInt n2.
override
moduloInt(int n2) Decimal
Euclidean modulo for int n2.
override
multiply(num n2) Decimal
override
multiplyBigInt(BigInt n2) Decimal
override
multiplyDecimal(Decimal other) DynamicNumber
override
multiplyDouble(double n2) Decimal
override
multiplyDynamicInt(DynamicInt other) DynamicNumber
override
multiplyInt(int n2) Decimal
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
power(DynamicNumber exponent) Decimal
This number to the power of exponent.
override
powerAsDecimal(DynamicNumber exponent) Decimal
This number to the power of DynamicNumber exponent as Decimal.
override
powerAsDynamicInt(DynamicNumber exponent) DynamicInt
This number to the power of DynamicNumber exponent as DynamicInt.
override
powerBigInt(BigInt exponent) DynamicNumber
This number to the power of exponent.
override
powerDouble(double exponent) DynamicNumber
This number to the power of exponent.
override
powerInt(int exponent) DynamicNumber
This number to the power of exponent.
override
powerNum(num exponent) DynamicNumber
This number to the power of exponent.
override
subtract(num n2) Decimal
override
subtractBigInt(BigInt amount) Decimal
override
subtractDouble(double n2) Decimal
override
subtractDynamicInt(DynamicInt other) Decimal
override
subtractInt(int amount) Decimal
override
sum(num n2) Decimal
override
sumBigInt(BigInt amount) Decimal
override
sumDouble(double amount) Decimal
override
sumDynamicInt(DynamicInt other) Decimal
override
sumInt(int amount) Decimal
override
toBigInt() BigInt
Returns this decimal as BigInt.
override
toDecimal() Decimal
Returns this number as Decimal.
override
toDouble() double
Returns this number as double.
override
toDynamicInt() DynamicInt
Returns this number as DynamicInt.
override
toHex() String
This number as an integer in hexadecimal format.
override
toInt() int
Returns this decimal as int.
override
toNum() num
Returns this decimal as num.
override
toString({bool compact = false, bool decimal = true, String decimalDelimiter = '.', bool thousands = false, String thousandsDelimiter = ','}) String
This decimal formatted to a String. Delegates to format.
override
toStringStandard() String
Formats this number to a String in a standard format, like int or double.
override
withHigherPrecision(Decimal other) Decimal
Returns this instance using the higher precision between this and other.
withLowerPrecision(Decimal other) Decimal
Returns this instance using the lower precision between this and other.
withMaximumPrecision(int maximumPrecision) Decimal
Ensures that precision is not gresater than maximumPrecision. Returns withPrecision(maximumPrecision) if needed.
withMinimumPrecision(int minimumPrecision) Decimal
Ensures that precision is not less than minimumPrecision. Returns withPrecision(minimumPrecision) if needed.
withPrecision(int precision) Decimal
Returns this instance with a new precision. Returns the same instance if precision is the same of the current precision.

Operators

operator %(DynamicNumber other) Decimal
Euclidean modulo operator.
override
operator *(DynamicNumber other) Decimal
override
operator +(DynamicNumber other) Decimal
override
operator -(DynamicNumber other) Decimal
override
operator /(DynamicNumber other) Decimal
override
operator <(DynamicNumber other) bool
override
operator <<(int shiftAmount) DynamicInt
Shift the bits of this integer to the left by shiftAmount.
override
operator <=(DynamicNumber other) bool
override
operator ==(Object other) bool
The equality operator.
override
operator >(DynamicNumber other) bool
override
operator >=(DynamicNumber other) bool
override
operator >>(int shiftAmount) DynamicInt
Shift the bits of this integer to the right by shiftAmount.
override
operator unary-() Decimal
Return the negative value of this integer.
override
operator ~() DynamicInt
The bit-wise negate operator.
override
operator ~/(DynamicNumber other) DynamicInt
override

Static Properties

one Decimal
final
ten Decimal
final
two Decimal
final
zero Decimal
final

Static Methods

from(Object? o, {int? precision}) Decimal?
override
tryParse(String s, {int? precision, String decimalDelimiter = '.'}) Decimal?
Tries to parse String s to a Decimal.
override