Table of Contents

Struct DecimalAmount

Namespace
Virtufin.Core.Position
Assembly
Virtufin.Core.dll
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

Whole long
Fraction ulong
N int

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

int

Properties

Fraction

public ulong Fraction { get; init; }

Property Value

ulong

N

public int N { get; init; }

Property Value

int

Whole

public long Whole { get; init; }

Property Value

long

Zero

A zero amount at DefaultPrecision precision.

public static DecimalAmount Zero { get; }

Property Value

DecimalAmount

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

other DecimalAmount

An 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 other in the sort order.
Zero This instance occurs in the same position in the sort order as other.
Greater than zero This instance follows other in 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

other DecimalAmount

An object to compare with this object.

Returns

bool

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

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

value decimal

Source value.

n int

Precision (number of fractional digits).

Returns

DecimalAmount

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

value double

Source value.

n int

Precision (number of fractional digits).

Returns

DecimalAmount

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

cardinal long

Cardinal value to decompose.

Returns

(long Whole, ulong Fraction)

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

cardinal long

Cardinal value to decompose.

n int

Precision the cardinal is expressed at.

Returns

(long Whole, ulong Fraction)

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

(long Whole, ulong Fraction)

ToCardinal()

Compute the cardinal (single-integer) representation at this amount's precision.

public long ToCardinal()

Returns

long

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

n int

Target precision (fractional digits).

Returns

long

ToDecimal()

Convert to decimal exactly.

public decimal ToDecimal()

Returns

decimal

ToString()

Format the amount as a decimal string.

public override string ToString()

Returns

string

Operators

operator +(DecimalAmount, DecimalAmount)

Add two amounts. Throws OverflowException on overflow.

public static DecimalAmount operator +(DecimalAmount left, DecimalAmount right)

Parameters

left DecimalAmount
right DecimalAmount

Returns

DecimalAmount

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

left DecimalAmount
right DecimalAmount

Returns

DecimalAmount

operator *(DecimalAmount, DecimalAmount)

Multiply two amounts of equal precision. Throws OverflowException on overflow.

public static DecimalAmount operator *(DecimalAmount left, DecimalAmount right)

Parameters

left DecimalAmount
right DecimalAmount

Returns

DecimalAmount

operator -(DecimalAmount, DecimalAmount)

Subtract two amounts. Throws OverflowException on overflow.

public static DecimalAmount operator -(DecimalAmount left, DecimalAmount right)

Parameters

left DecimalAmount
right DecimalAmount

Returns

DecimalAmount