MonetaryContext Class
Definition
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
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
otherMonetaryContextAn object to compare with this object.
Returns
Equals(object?)
Determines whether the specified object is equal to the current object.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current object.
Returns
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
currencyMinorUnitsintThe currency's declared minor-unit precision.
Returns
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
valuedecimalThe raw amount to round.
currencyMinorUnitsintThe currency's declared minor-unit precision.
Returns
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
leftMonetaryContextrightMonetaryContext
Returns
operator !=(MonetaryContext?, MonetaryContext?)
public static bool operator !=(MonetaryContext? left, MonetaryContext? right)
Parameters
leftMonetaryContextrightMonetaryContext
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |