Table of Contents

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

decimal

Currency

Gets the ISO 4217 currency code.

public CurrencyCode Currency { get; }

Property Value

CurrencyCode

Methods

Add(Money)

Adds two Money amounts if they have the same currency.

public Result<Money> Add(Money other)

Parameters

other Money

Returns

Result<Money>

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

ratios int[]

The ratios to split by (e.g., [1, 2, 1] for 25%, 50%, 25%).

Returns

Result<Money[]>

Array of Money amounts matching the ratios.

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

amount decimal

The monetary amount.

currencyCode string

ISO 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

divisor decimal

Returns

Result<Money>

Divide(int)

Divides the money amount by an integer to split evenly.

public Result<Money> Divide(int divisor)

Parameters

divisor int

Returns

Result<Money>

GetEqualityComponents()

Gets the equality components for value comparison.

protected override IEnumerable<IComparable> GetEqualityComponents()

Returns

IEnumerable<IComparable>

IsGreaterThan(Money)

Checks if this money is greater than another money amount (same currency required).

public bool IsGreaterThan(Money other)

Parameters

other Money

Returns

bool

IsGreaterThanOrEqual(Money)

Checks if this money is greater than or equal to another amount (same currency required).

public bool IsGreaterThanOrEqual(Money other)

Parameters

other Money

Returns

bool

IsLessThan(Money)

Checks if this money is less than another money amount (same currency required).

public bool IsLessThan(Money other)

Parameters

other Money

Returns

bool

IsLessThanOrEqual(Money)

Checks if this money is less than or equal to another amount (same currency required).

public bool IsLessThanOrEqual(Money other)

Parameters

other Money

Returns

bool

Multiply(decimal)

Multiplies the money amount by a scalar value.

public Result<Money> Multiply(decimal multiplier)

Parameters

multiplier decimal

Returns

Result<Money>

Multiply(int)

Multiplies the money amount by an integer quantity.

public Result<Money> Multiply(int quantity)

Parameters

quantity int

Returns

Result<Money>

Subtract(Money)

Subtracts another Money amount if they have the same currency.

public Result<Money> Subtract(Money other)

Parameters

other Money

Returns

Result<Money>

ToString()

Returns a string representation of the money amount with currency.

public override string ToString()

Returns

string

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

amount decimal

The monetary amount.

currencyCode string

ISO 4217 currency code (e.g., "USD", "EUR").

fieldName string

Optional field name for validation errors.

Returns

Result<Money>

Result containing the Money instance or validation errors.

Zero(string)

Creates a zero-value Money instance for the specified currency.

public static Result<Money> Zero(string currencyCode = "USD")

Parameters

currencyCode string

Returns

Result<Money>