Class Money
- Namespace
- Trellis.Primitives
- Assembly
- Trellis.Primitives.dll
Represents a monetary amount with a currency code. Provides type-safe arithmetic operations that prevent mixing different currencies.
[JsonConverter(typeof(MoneyJsonConverter))]
public class Money : ValueObject, IComparable<ValueObject>, IEquatable<ValueObject>
- Inheritance
-
Money
- Implements
- Inherited Members
- Extension Methods
Remarks
Money uses decimal for precision and stores amounts in the currency's base unit. All arithmetic operations enforce currency matching and return Result for error handling. Amounts are automatically rounded to the appropriate number of decimal places for the currency.
Properties
Amount
Gets the monetary amount in the currency's base unit.
public decimal Amount { get; }
Property Value
Currency
Gets the ISO 4217 currency code.
public CurrencyCode Currency { get; }
Property Value
Methods
Add(Money)
Adds two Money amounts if they have the same currency.
public Result<Money> Add(Money other)
Parameters
otherMoney
Returns
Allocate(params int[])
Allocates money across multiple parts using ratios. Ensures no money is lost to rounding by allocating remainder to first portions.
public Result<Money[]> Allocate(params int[] ratios)
Parameters
ratiosint[]The ratios to split by (e.g., [1, 2, 1] for 25%, 50%, 25%).
Returns
Create(decimal, string)
Creates a Money instance with the specified amount and currency code. Throws an exception if the amount or currency is invalid.
public static Money Create(decimal amount, string currencyCode)
Parameters
amountdecimalThe monetary amount.
currencyCodestringISO 4217 currency code (e.g., "USD", "EUR").
Returns
- Money
The Money instance.
Remarks
Use this method when you know the values are valid (e.g., in tests or with constants). For user input, use TryCreate(decimal, string, string?) instead.
Exceptions
- InvalidOperationException
Thrown when amount is negative or currency is invalid.
Divide(decimal)
Divides the money amount by a scalar value.
public Result<Money> Divide(decimal divisor)
Parameters
divisordecimal
Returns
Divide(int)
Divides the money amount by an integer to split evenly.
public Result<Money> Divide(int divisor)
Parameters
divisorint
Returns
GetEqualityComponents()
Gets the equality components for value comparison.
protected override IEnumerable<IComparable> GetEqualityComponents()
Returns
IsGreaterThan(Money)
Checks if this money is greater than another money amount (same currency required).
public bool IsGreaterThan(Money other)
Parameters
otherMoney
Returns
IsGreaterThanOrEqual(Money)
Checks if this money is greater than or equal to another amount (same currency required).
public bool IsGreaterThanOrEqual(Money other)
Parameters
otherMoney
Returns
IsLessThan(Money)
Checks if this money is less than another money amount (same currency required).
public bool IsLessThan(Money other)
Parameters
otherMoney
Returns
IsLessThanOrEqual(Money)
Checks if this money is less than or equal to another amount (same currency required).
public bool IsLessThanOrEqual(Money other)
Parameters
otherMoney
Returns
Multiply(decimal)
Multiplies the money amount by a scalar value.
public Result<Money> Multiply(decimal multiplier)
Parameters
multiplierdecimal
Returns
Multiply(int)
Multiplies the money amount by an integer quantity.
public Result<Money> Multiply(int quantity)
Parameters
quantityint
Returns
Subtract(Money)
Subtracts another Money amount if they have the same currency.
public Result<Money> Subtract(Money other)
Parameters
otherMoney
Returns
ToString()
Returns a string representation of the money amount with currency.
public override string ToString()
Returns
TryCreate(decimal, string, string?)
Creates a Money instance with the specified amount and currency code.
public static Result<Money> TryCreate(decimal amount, string currencyCode, string? fieldName = null)
Parameters
amountdecimalThe monetary amount.
currencyCodestringISO 4217 currency code (e.g., "USD", "EUR").
fieldNamestringOptional field name for validation errors.
Returns
Zero(string)
Creates a zero-value Money instance for the specified currency.
public static Result<Money> Zero(string currencyCode = "USD")
Parameters
currencyCodestring