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.fromJson(Map<String, dynamic> json)
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
isNonZero bool
Returns true when amount of this money is not zero.
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
copyWith({Fixed? amount, String? isoCode, int? decimalDigits}) Money
Creates a copy of this Money object with optional new values for amount, currency and decimalDigits. Changing the currency does NOT do an exchange calculation the amount is copied across verbatium (e.g. $1.00 AUD > $1.00 USD). If you pass an isoCode and it is invalid then a UnknownCurrencyException is thrown.
decimalPartAsString() String
divideByFixed(Fixed divisor) 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 <T>.
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
multipliedByPercentage(Percentage percentage) Money
Multiples this by the given percentage $1 * 20% = $0.20
multiplyByFixed(Fixed multiplier) Money
multiplyByNum(num multiplier, [int? decimalDigits = 16]) Money
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
percentageOf(Money base) Percentage
Calculates the percentage that this is of base. So this:10, base: 100 yeilds 0.1 which is 10%. The no. of decimal digits of the result is the larger of this and base
toDecimal() → Decimal
The value of the money as a Decimal.
toDouble() double
returns the value of the Money as a double. Becareful as you lose precision using a double You should store money as a integer and a scale.
toFixed() Fixed
The value of the money as a Fixed.
toInt() int
the integer (major part) of the money value as an int.
toJson() Map<String, dynamic>
Returns a JSON representation of this Money instance.
toString() String
A string representation of this object.
override

Operators

operator *(num multiplier) Money
Returns Money multiplied by multiplier, using schoolbook rounding. As we have no information about the scale of the multiplier we treated it as if it has 16 decimal places. Use multiplyByNum if want to explicitly control the number of decimal places considered in multiplier.
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 ==(Object 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