Struct Fixed
Represents a lightweight base-10 real number with fixed decimal Precision.
public readonly struct Fixed : IEquatable<Fixed>, IComparable<Fixed>, IComparable, IFormattable
- Implements
- Inherited Members
- Extension Methods
Fields
Epsilon
Represents smallest possible number greater than 0.
public static readonly Fixed Epsilon
Field Value
MaxPrecision
Maximum allowed precision.
public const byte MaxPrecision = 18
Field Value
MaxValue
Represents an integer equal to MaxValue.
public static readonly Fixed MaxValue
Field Value
MinPrecision
Minimum allowed precision. Used for integers.
public const byte MinPrecision = 0
Field Value
MinValue
Represents an integer equal to MinValue.
public static readonly Fixed MinValue
Field Value
Zero
Represents an integer equal to 0.
public static readonly Fixed Zero
Field Value
Properties
Precision
Represents the decimal precision of this instance.
public byte Precision { get; }
Property Value
RawValue
Represents the raw underlying value of this instance. The actual value is equal to raw value divided by 10^Precision.
public long RawValue { get; }
Property Value
Methods
Abs()
Creates a new Fixed instance by calculating an absolute value from this instance.
[Pure]
public Fixed Abs()
Returns
Add(Fixed)
Creates a new Fixed instance by adding other to this instance.
[Pure]
public Fixed Add(Fixed other)
Parameters
otherFixedOther instance to add.
Returns
AddRaw(long)
[Pure]
public Fixed AddRaw(long rawValue)
Parameters
rawValuelongRaw value to add.
Returns
Ceiling()
Creates a new Fixed instance by calculating the ceiling of this instance.
[Pure]
public Fixed Ceiling()
Returns
CompareTo(Fixed)
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.
[Pure]
public int CompareTo(Fixed other)
Parameters
otherFixedAn 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.
CompareTo(object?)
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.
[Pure]
public int CompareTo(object? obj)
Parameters
objobjectAn 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 objin the sort order.Zero This instance occurs in the same position in the sort order as obj.Greater than zero This instance follows objin the sort order.
Exceptions
- ArgumentException
objis not the same type as this instance.
Create(decimal, byte, MidpointRounding)
[Pure]
public static Fixed Create(decimal value, byte precision, MidpointRounding rounding = MidpointRounding.AwayFromZero)
Parameters
valuedecimalDecimal value.
precisionbyteDecimal precision.
roundingMidpointRoundingOptional
valuerounding strategy. Equal to AwayFromZero by default.
Returns
Exceptions
- ArgumentOutOfRangeException
When
precisionis less than MinPrecision or greater than MaxPrecision.
Create(double, byte, MidpointRounding)
[Pure]
public static Fixed Create(double value, byte precision, MidpointRounding rounding = MidpointRounding.AwayFromZero)
Parameters
valuedoubleDouble value.
precisionbyteDecimal precision.
roundingMidpointRoundingOptional
valuerounding strategy. Equal to AwayFromZero by default.
Returns
Exceptions
- ArgumentOutOfRangeException
When
precisionis less than MinPrecision or greater than MaxPrecision.
Create(long, byte)
[Pure]
public static Fixed Create(long value, byte precision = 0)
Parameters
valuelongInteger value.
precisionbyteDecimal precision. Equal to MinPrecision by default.
Returns
Exceptions
- ArgumentOutOfRangeException
When
precisionis less than MinPrecision or greater than MaxPrecision.
CreateEpsilon(byte)
Creates a new Fixed instance equivalent to the smallest possible value greater than 0
with the provided precision.
[Pure]
public static Fixed CreateEpsilon(byte precision)
Parameters
precisionbyteDecimal precision.
Returns
Exceptions
- ArgumentOutOfRangeException
When
precisionis less than MinPrecision or greater than MaxPrecision.
CreateMaxValue(byte)
Creates a new Fixed instance equivalent to maximum possible value with the provided precision.
[Pure]
public static Fixed CreateMaxValue(byte precision)
Parameters
precisionbyteDecimal precision.
Returns
Exceptions
- ArgumentOutOfRangeException
When
precisionis less than MinPrecision or greater than MaxPrecision.
CreateMinValue(byte)
Creates a new Fixed instance equivalent to minimum possible value with the provided precision.
[Pure]
public static Fixed CreateMinValue(byte precision)
Parameters
precisionbyteDecimal precision.
Returns
Exceptions
- ArgumentOutOfRangeException
When
precisionis less than MinPrecision or greater than MaxPrecision.
CreateRaw(long, byte)
Creates a new Fixed instance with the provided rawValue and precision.
[Pure]
public static Fixed CreateRaw(long rawValue, byte precision)
Parameters
Returns
Exceptions
- ArgumentOutOfRangeException
When
precisionis less than MinPrecision or greater than MaxPrecision.
CreateZero(byte)
Creates a new Fixed instance equivalent to 0 with the provided precision.
[Pure]
public static Fixed CreateZero(byte precision)
Parameters
precisionbyteDecimal precision.
Returns
Exceptions
- ArgumentOutOfRangeException
When
precisionis less than MinPrecision or greater than MaxPrecision.
Decrement()
Creates a new Fixed instance by subtracting 1 from this instance.
[Pure]
public Fixed Decrement()
Returns
Divide(Fixed)
Creates a new Fixed instance by dividing this instance by other.
[Pure]
public Fixed Divide(Fixed other)
Parameters
otherFixedOther instance to divide by.
Returns
Exceptions
- DivideByZeroException
When
otheris equal to 0.
DivideRaw(long)
[Pure]
public Fixed DivideRaw(long rawValue)
Parameters
rawValuelongRaw value to divide by.
Returns
Exceptions
- DivideByZeroException
When
rawValueis equal to 0.
Equals(Fixed)
Indicates whether the current object is equal to another object of the same type.
[Pure]
public bool Equals(Fixed other)
Parameters
otherFixedAn object to compare with this object.
Returns
Equals(object?)
Indicates whether this instance and a specified object are equal.
[Pure]
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current instance.
Returns
- bool
true if
objand this instance are the same type and represent the same value; otherwise, false.
Floor()
Creates a new Fixed instance by calculating the floor of this instance.
[Pure]
public Fixed Floor()
Returns
GetHashCode()
Returns the hash code for this instance.
[Pure]
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
GetScale(byte)
Returns 10^precision.
[Pure]
public static long GetScale(byte precision)
Parameters
precisionbyteDecimal precision to calculate scale for.
Returns
- long
10^
precision.
Exceptions
- IndexOutOfRangeException
When
precisionis less than MinPrecision or greater than MaxPrecision.
Increment()
Creates a new Fixed instance by adding 1 to this instance.
[Pure]
public Fixed Increment()
Returns
Modulo(Fixed)
Creates a new Fixed instance by calculating the remainder of division of this instance by other.
[Pure]
public Fixed Modulo(Fixed other)
Parameters
otherFixedOther instance to divide by.
Returns
Exceptions
- DivideByZeroException
When
otheris equal to 0.
ModuloRaw(long)
Creates a new Fixed instance by calculating the remainder of division of
the RawValue of this instance by rawValue.
[Pure]
public Fixed ModuloRaw(long rawValue)
Parameters
rawValuelongRaw value to divide by.
Returns
Exceptions
- DivideByZeroException
When
rawValueis equal to 0.
Multiply(Fixed)
Creates a new Fixed instance by multiplying other and this instance together.
[Pure]
public Fixed Multiply(Fixed other)
Parameters
otherFixedOther instance to multiply by.
Returns
Multiply(Percent)
Creates a new Fixed instance by multiplying percent and this instance together.
[Pure]
public Fixed Multiply(Percent percent)
Parameters
Returns
MultiplyRaw(long)
[Pure]
public Fixed MultiplyRaw(long rawValue)
Parameters
rawValuelongRaw value to multiply by.
Returns
Negate()
Creates a new Fixed instance by negating this instance.
[Pure]
public Fixed Negate()
Returns
Round(int)
Creates a new Fixed instance by rounding this instance to the provided precision.
[Pure]
public Fixed Round(int precision)
Parameters
precisionintDecimal precision to round to.
Returns
Remarks
Uses AwayFromZero rounding strategy.
Exceptions
- ArgumentOutOfRangeException
When
precisionis less than 0.
SetPrecision(byte)
[Pure]
public Fixed SetPrecision(byte precision)
Parameters
precisionbyteDecimal precision.
Returns
Remarks
Resulting value will be rounded using AwayFromZero strategy when new precision is less than the current one.
Exceptions
- ArgumentOutOfRangeException
When
precisionis less than MinPrecision or greater than MaxPrecision.
SetRawValue(long)
[Pure]
public Fixed SetRawValue(long rawValue)
Parameters
rawValuelongRaw value.
Returns
SetValue(decimal, MidpointRounding)
[Pure]
public Fixed SetValue(decimal value, MidpointRounding rounding = MidpointRounding.AwayFromZero)
Parameters
valuedecimalDecimal value.
roundingMidpointRoundingOptional
valuerounding strategy. Equal to AwayFromZero by default.
Returns
SetValue(double, MidpointRounding)
[Pure]
public Fixed SetValue(double value, MidpointRounding rounding = MidpointRounding.AwayFromZero)
Parameters
valuedoubleDouble value.
roundingMidpointRoundingOptional
valuerounding strategy. Equal to AwayFromZero by default.
Returns
SetValue(long)
[Pure]
public Fixed SetValue(long value)
Parameters
valuelongInteger value.
Returns
Subtract(Fixed)
Creates a new Fixed instance by subtracting other from this instance.
[Pure]
public Fixed Subtract(Fixed other)
Parameters
otherFixedOther instance to subtract.
Returns
SubtractRaw(long)
[Pure]
public Fixed SubtractRaw(long rawValue)
Parameters
rawValuelongRaw value to subtract.
Returns
ToString()
Returns a string representation of this Fixed instance.
[Pure]
public override string ToString()
Returns
- string
String representation.
ToString(IFormatProvider?)
Returns a string representation of this Fixed instance.
[Pure]
public string ToString(IFormatProvider? formatProvider)
Parameters
formatProviderIFormatProviderAn optional format provider.
Returns
- string
String representation.
ToString(string?, IFormatProvider?)
Returns a string representation of this Fixed instance.
[Pure]
public string ToString(string? format, IFormatProvider? formatProvider)
Parameters
formatstringAn optional numeric format.
formatProviderIFormatProviderAn optional format provider.
Returns
- string
String representation.
Truncate()
Creates a new Fixed instance by truncating this instance.
[Pure]
public Fixed Truncate()
Returns
Operators
operator +(Fixed, Fixed)
Creates a new Fixed instance by adding left and right together.
[Pure]
public static Fixed operator +(Fixed left, Fixed right)
Parameters
Returns
operator --(Fixed)
Creates a new Fixed instance by decrementing the provided f.
[Pure]
public static Fixed operator --(Fixed f)
Parameters
fFixedOperand.
Returns
operator /(Fixed, Fixed)
Creates a new Fixed instance by dividing left by right.
[Pure]
public static Fixed operator /(Fixed left, Fixed right)
Parameters
Returns
Exceptions
- DivideByZeroException
When
rightis equal to 0.
operator ==(Fixed, Fixed)
Checks if left is equal to right.
[Pure]
public static bool operator ==(Fixed left, Fixed right)
Parameters
Returns
- bool
true when operands are equal, otherwise false.
explicit operator double(Fixed)
Converts the provided f to double.
[Pure]
public static explicit operator double(Fixed f)
Parameters
fFixedNumber to convert.
Returns
explicit operator long(Fixed)
Converts the provided f to long through truncation.
[Pure]
public static explicit operator long(Fixed f)
Parameters
fFixedNumber to convert.
Returns
operator >(Fixed, Fixed)
Checks if left is greater than right.
[Pure]
public static bool operator >(Fixed left, Fixed right)
Parameters
Returns
- bool
true when
leftis greater thanright, otherwise false.
operator >=(Fixed, Fixed)
Checks if left is greater than or equal to right.
[Pure]
public static bool operator >=(Fixed left, Fixed right)
Parameters
Returns
- bool
true when
leftis greater than or equal toright, otherwise false.
implicit operator decimal(Fixed)
Converts the provided f to decimal.
[Pure]
public static implicit operator decimal(Fixed f)
Parameters
fFixedNumber to convert.
Returns
operator ++(Fixed)
Creates a new Fixed instance by incrementing the provided f.
[Pure]
public static Fixed operator ++(Fixed f)
Parameters
fFixedOperand.
Returns
operator !=(Fixed, Fixed)
Checks if left is not equal to right.
[Pure]
public static bool operator !=(Fixed left, Fixed right)
Parameters
Returns
- bool
true when operands are not equal, otherwise false.
operator <(Fixed, Fixed)
Checks if left is less than right.
[Pure]
public static bool operator <(Fixed left, Fixed right)
Parameters
Returns
- bool
true when
leftis less thanright, otherwise false.
operator <=(Fixed, Fixed)
Checks if left is less than or equal to right.
[Pure]
public static bool operator <=(Fixed left, Fixed right)
Parameters
Returns
- bool
true when
leftis less than or equal toright, otherwise false.
operator %(Fixed, Fixed)
Creates a new Fixed instance by calculating the remainder of division of
left by right.
[Pure]
public static Fixed operator %(Fixed left, Fixed right)
Parameters
Returns
Exceptions
- DivideByZeroException
When
rightis equal to 0.
operator *(Fixed, Fixed)
Creates a new Fixed instance by multiplying left and right together.
[Pure]
public static Fixed operator *(Fixed left, Fixed right)
Parameters
Returns
operator *(Fixed, Percent)
Creates a new Fixed instance by multiplying left and right together.
[Pure]
public static Fixed operator *(Fixed left, Percent right)
Parameters
Returns
operator *(Percent, Fixed)
Creates a new Fixed instance by multiplying left and right together.
[Pure]
public static Fixed operator *(Percent left, Fixed right)
Parameters
Returns
operator -(Fixed, Fixed)
Creates a new Fixed instance by subtracting right from left.
[Pure]
public static Fixed operator -(Fixed left, Fixed right)
Parameters
Returns
operator -(Fixed)
Creates a new Fixed instance by negating the provided f.
[Pure]
public static Fixed operator -(Fixed f)
Parameters
fFixedOperand.