Money class

Allows you to store, print and perform mathematically operations on money whilst maintaining precision.

NOTE: This is a value type, do not extend or re-implement it.

The Money class works with the Currency class to provide a simple means to define monetary values.

e.g.

Currency aud = Currency.create('AUD', 2, pattern:r'$0.00');
Money costPrice = Money.fromInt(1000, aud);
costPrice.toString();
> $10.00

Money taxInclusive = costPrice * 1.1;
taxInclusive.toString();
> $11.00

taxInclusive.format('SCCC0.00');
> $AUD11.00

taxInclusive.format('SCCC0');
> $AUD11

Money uses BigInt internally to represent an amount in minorUnits (e.g. cents)

Implemented types
Annotations
  • @immutable

Constructors

Money.fromBigInt(BigInt minorUnits, {required String isoCode, int? decimalDigits})
factory
Money.fromBigIntWithCurrency(BigInt minorUnits, Currency currency, {int? decimalDigits})
Creates an instance of Money from an amount represented by minorUnits which is in the minorUnits of the currency, e.g (cents).
factory
Money.fromDecimal(Decimal amount, {required String isoCode, int? decimalDigits})
Creates a Money from a Decimal amount.
factory
Money.fromDecimalWithCurrency(Decimal amount, Currency currency, {int? decimalDigits})
factory
Money.fromFixed(Fixed amount, {required String isoCode, int? decimalDigits})
Creates a Money from a Fixed amount.
factory
Money.fromFixedWithCurrency(Fixed amount, Currency currency, {int? decimalDigits})
Creates a Money from a Fixed amount.
factory
Money.fromInt(int minorUnits, {required String isoCode, int? decimalDigits})
factory
Money.fromIntWithCurrency(int minorUnits, Currency currency, {int? decimalDigits})
Creates an instance of Money from an integer.
factory
Money.fromNum(num amount, {required String isoCode, int? decimalDigits})
factory
Money.fromNumWithCurrency(num amount, Currency currency, {int? decimalDigits})
Creates an instance of Money from a num holding the monetary value.
factory
Money.parse(String amount, {required String isoCode, String? pattern, int? decimalDigits})
factory
Money.parseWithCurrency(String amount, Currency currency, {String? pattern, int? decimalDigits})
Parses the passed amount and returns a Money instance.
factory

Properties

amount Fixed
The monetary amount
final
currency Currency
The currency the amount is stored in.
final
decimalDigits int
no setter
decimalPart BigInt
The component of the number after the decimal point.
no setter
hashCode int
The hash code for this object.
no setteroverride
integerPart BigInt
The component of the number before the decimal point
no setter
isNegative bool
Returns true when amount of this money is negative.
no setter
isPositive bool
Returns true when amount of this money is positive (greater than zero).
no setter
isZero bool
Returns true when amount of this money is zero.
no setter
minorUnits BigInt
Returns the underlying minorUnits for this monetary amount. e.g. $10.10 is returned as 1010
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
sign int
Returns the sign of this Fixed amount. Returns 0 for zero, -1 for values less than zero and +1 for values greater than zero.
no setter

Methods

allocationAccordingTo(List<int> ratios) List<Money>
Returns allocation of this money according to ratios.
allocationTo(int targets) List<Money>
Returns allocation of this money to N targets.
compareTo(Money other) int
Compares this to other.
override
divideByFixed(Fixed multiplier) Money
dividedBy(Money divisor) double
Divides this by divisor and returns the result as a double
encodedBy<T>(MoneyEncoder<T> encoder) → T
Encodes a Money instance as a
exchangeTo(ExchangeRate exchangeRate) Money
Converts a Money instance into a new Money instance with its Currency defined by the exchangeRate.
format(String pattern) String
Provides a simple means of formating a Money instance as a string.
isInCurrency(String isoCode) bool
Returns true if this money value is in the specified currency.
isInCurrencyWithCurrency(Currency other) bool
isInSameCurrencyAs(Money other) bool
Returns true if this money value is in same currency as other.
modulusFixed(Fixed other) Money
multiplyByFixed(Fixed multiplier) Money
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
override

Operators

operator *(num multiplier) Money
Returns Money multiplied by multiplier, using schoolbook rounding.
operator +(Money summand) Money
Adds operands.
operator -(Money subtrahend) Money
Subtracts right operand from the left one.
operator /(num divisor) Money
Returns Money divided by divisor, using schoolbook rounding.
operator <(Money other) bool
Returns true when this money is less than other.
operator <=(Money other) bool
Returns true when this money is less than or equal to other.
operator ==(covariant Money other) bool
Returns true if other is the same amount of money in the same currency.
override
operator >(Money other) bool
Returns true when this money is greater than other.
operator >=(Money other) bool
Returns true when this money is greater than or equal to other.
operator unary-() Money
unary minus operator.

Static Methods

decoding<T>(T value, MoneyDecoder<T> decoder) Money
Returns a Money instance decoded from value by decoder.
tryParse(String monetaryAmount, {required String isoCode, String? pattern, int? decimalDigits}) Money?
The same as Money.parse but returns null if we are unable to Money.parse the monetaryAmount