Struct DecimalAmount
public readonly record struct DecimalAmount : IEquatable<DecimalAmount>, IComparable<DecimalAmount>, IAmount<(long, ulong)>, IEquatable<IAmount<(long, ulong)>>, IComparable<IAmount<(long, ulong)>>, IAsCardinal<long>, IAdditionOperators<DecimalAmount, DecimalAmount, DecimalAmount>, ISubtractionOperators<DecimalAmount, DecimalAmount, DecimalAmount>, IMultiplyOperators<DecimalAmount, DecimalAmount, DecimalAmount>, IDivisionOperators<DecimalAmount, DecimalAmount, DecimalAmount>
- Implements
- Inherited Members
Constructors
DecimalAmount(long, ulong, int)
public DecimalAmount(long Whole, ulong Fraction, int N)
Parameters
Fields
DefaultPrecision
Default precision (fractional digits) for newly constructed amounts when no precision is supplied. Matches conventional currency representation.
public const int DefaultPrecision = 2
Field Value
Properties
Fraction
public ulong Fraction { get; init; }
Property Value
N
public int N { get; init; }
Property Value
Whole
public long Whole { get; init; }
Property Value
Zero
A zero amount at DefaultPrecision precision.
public static DecimalAmount Zero { get; }
Property Value
Methods
CompareTo(DecimalAmount)
Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
public int CompareTo(DecimalAmount other)
Parameters
otherDecimalAmountAn object to compare with this instance.
Returns
- int
A value that indicates the relative order of the objects being compared. The return value has these meanings:
Value Meaning Less than zero This instance precedes otherin the sort order.Zero This instance occurs in the same position in the sort order as other.Greater than zero This instance follows otherin the sort order.
Equals(DecimalAmount)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(DecimalAmount other)
Parameters
otherDecimalAmountAn object to compare with this object.
Returns
From(decimal, int)
Construct a DecimalAmount from a decimal. This is the exact constructor — no double round-trip.
public static DecimalAmount From(decimal value, int n)
Parameters
Returns
From(double, int)
Construct a DecimalAmount from a double. Prefer From(decimal, int) for exact values; this overload exists for call sites that already hold a double.
public static DecimalAmount From(double value, int n)
Parameters
Returns
FromCardinal(long)
Recover whole and fractional parts from a cardinal value at this amount's precision.
public (long Whole, ulong Fraction) FromCardinal(long cardinal)
Parameters
cardinallongCardinal value to decompose.
Returns
FromCardinal(long, int)
Recover whole and fractional parts from a cardinal value at an explicit precision.
public static (long Whole, ulong Fraction) FromCardinal(long cardinal, int n)
Parameters
Returns
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
GetValue()
public (long Whole, ulong Fraction) GetValue()
Returns
ToCardinal()
Compute the cardinal (single-integer) representation at this amount's precision.
public long ToCardinal()
Returns
ToCardinal(int)
Compute the cardinal (single-integer) representation at an explicit
precision. Rescales when n differs from
N, rounding half away from zero.
public long ToCardinal(int n)
Parameters
nintTarget precision (fractional digits).
Returns
ToDecimal()
Convert to decimal exactly.
public decimal ToDecimal()
Returns
ToString()
Format the amount as a decimal string.
public override string ToString()
Returns
Operators
operator +(DecimalAmount, DecimalAmount)
Add two amounts. Throws OverflowException on overflow.
public static DecimalAmount operator +(DecimalAmount left, DecimalAmount right)
Parameters
leftDecimalAmountrightDecimalAmount
Returns
operator /(DecimalAmount, DecimalAmount)
Divide two amounts of equal precision. Throws DivideByZeroException when the divisor is zero.
public static DecimalAmount operator /(DecimalAmount left, DecimalAmount right)
Parameters
leftDecimalAmountrightDecimalAmount
Returns
operator *(DecimalAmount, DecimalAmount)
Multiply two amounts of equal precision. Throws OverflowException on overflow.
public static DecimalAmount operator *(DecimalAmount left, DecimalAmount right)
Parameters
leftDecimalAmountrightDecimalAmount
Returns
operator -(DecimalAmount, DecimalAmount)
Subtract two amounts. Throws OverflowException on overflow.
public static DecimalAmount operator -(DecimalAmount left, DecimalAmount right)
Parameters
leftDecimalAmountrightDecimalAmount