Table of Contents

MonetaryContext Class

Definition

Namespace
Bodu.Financial
Assembly
Bodu.Financial.dll
Package
Bodu.Financial 1.0.0
Source
MonetaryContext.cs

Carries the rounding and scaling policy applied at a monetary operation boundary - multiplication, division, conversion, allocation, and the conversion of a high-precision calculation back to a settlement value.

public sealed record MonetaryContext : IEquatable<MonetaryContext>
Inheritance
MonetaryContext
Implements
Inherited Members
Extension Methods

Remarks

A MonetaryContext is supplied to operations rather than embedded in a Money value, so the same amount can participate in calculations under different policies without carrying mutable state. Operations that accept a nullable context treat null as Default, preserving the historical banker's-rounding behaviour.

using Bodu.Financial;

// Start from the default and override only the policies you need.
MonetaryContext retail = MonetaryContext.Default with
{
    Rounding = new MidpointRoundingStrategy(MidpointRounding.AwayFromZero),
    Allocation = AllocationPolicy.LargestRemainder,
};

// Apply it at a settlement boundary.
var calc = new CalculatedMoney(2.125m, CurrencyCode.USD);
Money settled = calc.RoundToMoney(retail);   // 2.13 USD (rounded away from zero)

Constructors

MonetaryContext()

public MonetaryContext()

Properties

Allocation

Gets the allocation policy used when distributing an amount across shares.

public AllocationPolicy Allocation { get; init; }

Property Value

AllocationPolicy

The configured AllocationPolicy.

CashRounding

Gets the cash-rounding policy applied to results.

public CashRoundingPolicy CashRounding { get; init; }

Property Value

CashRoundingPolicy

The configured CashRoundingPolicy.

ConversionRounding

Gets the policy that determines when a currency conversion is rounded.

public ConversionRoundingPolicy ConversionRounding { get; init; }

Property Value

ConversionRoundingPolicy

The configured ConversionRoundingPolicy.

CustomScale

Gets the explicit scale used when ScalePolicy is Custom.

public int? CustomScale { get; init; }

Property Value

int?

The custom scale, or null when not applicable.

Default

Gets the shared default context: banker's rounding to the currency's minor units, no cash rounding, largest-remainder allocation, and rounding conversions at the target scale.

public static MonetaryContext Default { get; }

Property Value

MonetaryContext

The default MonetaryContext.

Rounding

Gets the rounding strategy applied when reducing a computed amount to the resolved scale.

public IRoundingStrategy Rounding { get; init; }

Property Value

IRoundingStrategy

The rounding strategy; never null.

ScalePolicy

Gets the policy that determines the fractional-digit scale results are rounded to.

public ScalePolicy ScalePolicy { get; init; }

Property Value

ScalePolicy

The configured ScalePolicy.

Methods

Equals(MonetaryContext?)

Indicates whether the current object is equal to another object of the same type.

public bool Equals(MonetaryContext? other)

Parameters

other MonetaryContext

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Determines whether the specified object is equal to the current object.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current object.

Returns

bool

true if the specified object is equal to the current object; otherwise, false.

GetHashCode()

Serves as the default hash function.

public override int GetHashCode()

Returns

int

A hash code for the current object.

ResolveScale(int)

Resolves the effective fractional-digit scale for a currency with currencyMinorUnits minor units.

public int ResolveScale(int currencyMinorUnits)

Parameters

currencyMinorUnits int

The currency's declared minor-unit precision.

Returns

int

The number of fractional digits to round to, or -1 when Unrounded defers rounding entirely.

Exceptions

ArgumentOutOfRangeException

ScalePolicy is not a defined value.

ArgumentException

ScalePolicy is Custom but CustomScale is null.

Round(decimal, int)

Rounds value for a currency with currencyMinorUnits minor units using this context's rounding strategy and resolved scale.

public decimal Round(decimal value, int currencyMinorUnits)

Parameters

value decimal

The raw amount to round.

currencyMinorUnits int

The currency's declared minor-unit precision.

Returns

decimal

The rounded amount, or value unchanged when Unrounded is in effect.

ToString()

Returns a string that represents the current object.

public override string ToString()

Returns

string

A string that represents the current object.

Validate()

Validates that every policy member is a defined value and that CustomScale is supplied when required.

public void Validate()

Exceptions

ArgumentOutOfRangeException

A policy member is not a defined value.

ArgumentException

ScalePolicy is Custom but CustomScale is null, or it is out of the range 0 to 28.

Operators

operator ==(MonetaryContext?, MonetaryContext?)

public static bool operator ==(MonetaryContext? left, MonetaryContext? right)

Parameters

left MonetaryContext
right MonetaryContext

Returns

bool

operator !=(MonetaryContext?, MonetaryContext?)

public static bool operator !=(MonetaryContext? left, MonetaryContext? right)

Parameters

left MonetaryContext
right MonetaryContext

Returns

bool

Applies to

ProductVersions
.NET8, 10