Table of Contents

Struct Fixed

Namespace
LfrlAnvil.Numerics
Assembly
LfrlAnvil.Core.dll

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

Fixed

MaxPrecision

Maximum allowed precision.

public const byte MaxPrecision = 18

Field Value

byte

MaxValue

Represents an integer equal to MaxValue.

public static readonly Fixed MaxValue

Field Value

Fixed

MinPrecision

Minimum allowed precision. Used for integers.

public const byte MinPrecision = 0

Field Value

byte

MinValue

Represents an integer equal to MinValue.

public static readonly Fixed MinValue

Field Value

Fixed

Zero

Represents an integer equal to 0.

public static readonly Fixed Zero

Field Value

Fixed

Properties

Precision

Represents the decimal precision of this instance.

public byte Precision { get; }

Property Value

byte

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

long

Methods

Abs()

Creates a new Fixed instance by calculating an absolute value from this instance.

[Pure]
public Fixed Abs()

Returns

Fixed

New Fixed instance with unchanged Precision.

Add(Fixed)

Creates a new Fixed instance by adding other to this instance.

[Pure]
public Fixed Add(Fixed other)

Parameters

other Fixed

Other instance to add.

Returns

Fixed

New Fixed instance with Precision being the greater precision out of the two operands.

AddRaw(long)

Creates a new Fixed instance by adding rawValue to the RawValue of this instance.

[Pure]
public Fixed AddRaw(long rawValue)

Parameters

rawValue long

Raw value to add.

Returns

Fixed

New Fixed instance with unchanged Precision.

Ceiling()

Creates a new Fixed instance by calculating the ceiling of this instance.

[Pure]
public Fixed Ceiling()

Returns

Fixed

New Fixed instance with unchanged Precision.

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

other Fixed

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.

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

obj object

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

Exceptions

ArgumentException

obj is not the same type as this instance.

Create(decimal, byte, MidpointRounding)

Creates a new Fixed instance equivalent to the provided decimalvalue with the given precision.

[Pure]
public static Fixed Create(decimal value, byte precision, MidpointRounding rounding = MidpointRounding.AwayFromZero)

Parameters

value decimal

Decimal value.

precision byte

Decimal precision.

rounding MidpointRounding

Optional value rounding strategy. Equal to AwayFromZero by default.

Returns

Fixed

New Fixed instance.

Exceptions

ArgumentOutOfRangeException

When precision is less than MinPrecision or greater than MaxPrecision.

Create(double, byte, MidpointRounding)

Creates a new Fixed instance equivalent to the provided doublevalue with the given precision.

[Pure]
public static Fixed Create(double value, byte precision, MidpointRounding rounding = MidpointRounding.AwayFromZero)

Parameters

value double

Double value.

precision byte

Decimal precision.

rounding MidpointRounding

Optional value rounding strategy. Equal to AwayFromZero by default.

Returns

Fixed

New Fixed instance.

Exceptions

ArgumentOutOfRangeException

When precision is less than MinPrecision or greater than MaxPrecision.

Create(long, byte)

Creates a new Fixed instance equivalent to the provided longvalue with the given precision.

[Pure]
public static Fixed Create(long value, byte precision = 0)

Parameters

value long

Integer value.

precision byte

Decimal precision. Equal to MinPrecision by default.

Returns

Fixed

New Fixed instance.

Exceptions

ArgumentOutOfRangeException

When precision is 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

precision byte

Decimal precision.

Returns

Fixed

New Fixed instance.

Exceptions

ArgumentOutOfRangeException

When precision is 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

precision byte

Decimal precision.

Returns

Fixed

New Fixed instance.

Exceptions

ArgumentOutOfRangeException

When precision is 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

precision byte

Decimal precision.

Returns

Fixed

New Fixed instance.

Exceptions

ArgumentOutOfRangeException

When precision is 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

rawValue long

Raw value.

precision byte

Decimal precision.

Returns

Fixed

New Fixed instance.

Exceptions

ArgumentOutOfRangeException

When precision is 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

precision byte

Decimal precision.

Returns

Fixed

New Fixed instance.

Exceptions

ArgumentOutOfRangeException

When precision is less than MinPrecision or greater than MaxPrecision.

Decrement()

Creates a new Fixed instance by subtracting 1 from this instance.

[Pure]
public Fixed Decrement()

Returns

Fixed

New Fixed instance with unchanged Precision.

Divide(Fixed)

Creates a new Fixed instance by dividing this instance by other.

[Pure]
public Fixed Divide(Fixed other)

Parameters

other Fixed

Other instance to divide by.

Returns

Fixed

New Fixed instance with Precision being the greater precision out of the two operands.

Exceptions

DivideByZeroException

When other is equal to 0.

DivideRaw(long)

Creates a new Fixed instance by dividing the RawValue of this instance by rawValue.

[Pure]
public Fixed DivideRaw(long rawValue)

Parameters

rawValue long

Raw value to divide by.

Returns

Fixed

New Fixed instance with unchanged Precision.

Exceptions

DivideByZeroException

When rawValue is 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

other Fixed

An object to compare with this object.

Returns

bool

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

Equals(object?)

Indicates whether this instance and a specified object are equal.

[Pure]
public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current instance.

Returns

bool

true if obj and 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

Fixed

New Fixed instance with unchanged Precision.

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

precision byte

Decimal precision to calculate scale for.

Returns

long

10^ precision.

Exceptions

IndexOutOfRangeException

When precision is less than MinPrecision or greater than MaxPrecision.

Increment()

Creates a new Fixed instance by adding 1 to this instance.

[Pure]
public Fixed Increment()

Returns

Fixed

New Fixed instance with unchanged Precision.

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

other Fixed

Other instance to divide by.

Returns

Fixed

New Fixed instance with Precision being the greater precision out of the two operands.

Exceptions

DivideByZeroException

When other is 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

rawValue long

Raw value to divide by.

Returns

Fixed

New Fixed instance with unchanged Precision.

Exceptions

DivideByZeroException

When rawValue is equal to 0.

Multiply(Fixed)

Creates a new Fixed instance by multiplying other and this instance together.

[Pure]
public Fixed Multiply(Fixed other)

Parameters

other Fixed

Other instance to multiply by.

Returns

Fixed

New Fixed instance with Precision being the greater precision out of the two operands.

Multiply(Percent)

Creates a new Fixed instance by multiplying percent and this instance together.

[Pure]
public Fixed Multiply(Percent percent)

Parameters

percent Percent

Percent to multiply by.

Returns

Fixed

New Fixed instance with unchanged Precision.

MultiplyRaw(long)

Creates a new Fixed instance by multiplying rawValue by the RawValue of this instance.

[Pure]
public Fixed MultiplyRaw(long rawValue)

Parameters

rawValue long

Raw value to multiply by.

Returns

Fixed

New Fixed instance with unchanged Precision.

Negate()

Creates a new Fixed instance by negating this instance.

[Pure]
public Fixed Negate()

Returns

Fixed

New Fixed instance with unchanged Precision.

Round(int)

Creates a new Fixed instance by rounding this instance to the provided precision.

[Pure]
public Fixed Round(int precision)

Parameters

precision int

Decimal precision to round to.

Returns

Fixed

New Fixed instance with unchanged Precision.

Remarks

Uses AwayFromZero rounding strategy.

Exceptions

ArgumentOutOfRangeException

When precision is less than 0.

SetPrecision(byte)

Creates a new Fixed instance with changed Precision.

[Pure]
public Fixed SetPrecision(byte precision)

Parameters

precision byte

Decimal precision.

Returns

Fixed

New Fixed instance.

Remarks

Resulting value will be rounded using AwayFromZero strategy when new precision is less than the current one.

Exceptions

ArgumentOutOfRangeException

When precision is less than MinPrecision or greater than MaxPrecision.

SetRawValue(long)

Creates a new Fixed instance with changed RawValue.

[Pure]
public Fixed SetRawValue(long rawValue)

Parameters

rawValue long

Raw value.

Returns

Fixed

New Fixed instance with unchanged Precision.

SetValue(decimal, MidpointRounding)

Creates a new Fixed instance equivalent to the provided decimalvalue.

[Pure]
public Fixed SetValue(decimal value, MidpointRounding rounding = MidpointRounding.AwayFromZero)

Parameters

value decimal

Decimal value.

rounding MidpointRounding

Optional value rounding strategy. Equal to AwayFromZero by default.

Returns

Fixed

New Fixed instance with unchanged Precision.

SetValue(double, MidpointRounding)

Creates a new Fixed instance equivalent to the provided doublevalue.

[Pure]
public Fixed SetValue(double value, MidpointRounding rounding = MidpointRounding.AwayFromZero)

Parameters

value double

Double value.

rounding MidpointRounding

Optional value rounding strategy. Equal to AwayFromZero by default.

Returns

Fixed

New Fixed instance with unchanged Precision.

SetValue(long)

Creates a new Fixed instance equivalent to the provided longvalue.

[Pure]
public Fixed SetValue(long value)

Parameters

value long

Integer value.

Returns

Fixed

New Fixed instance with unchanged Precision.

Subtract(Fixed)

Creates a new Fixed instance by subtracting other from this instance.

[Pure]
public Fixed Subtract(Fixed other)

Parameters

other Fixed

Other instance to subtract.

Returns

Fixed

New Fixed instance with Precision being the greater precision out of the two operands.

SubtractRaw(long)

Creates a new Fixed instance by subtracting rawValue from the RawValue of this instance.

[Pure]
public Fixed SubtractRaw(long rawValue)

Parameters

rawValue long

Raw value to subtract.

Returns

Fixed

New Fixed instance with unchanged Precision.

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

formatProvider IFormatProvider

An 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

format string

An optional numeric format.

formatProvider IFormatProvider

An optional format provider.

Returns

string

String representation.

Truncate()

Creates a new Fixed instance by truncating this instance.

[Pure]
public Fixed Truncate()

Returns

Fixed

New Fixed instance with unchanged Precision.

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

left Fixed

First operand.

right Fixed

Second operand.

Returns

Fixed

New Fixed instance.

operator --(Fixed)

Creates a new Fixed instance by decrementing the provided f.

[Pure]
public static Fixed operator --(Fixed f)

Parameters

f Fixed

Operand.

Returns

Fixed

New Fixed instance.

operator /(Fixed, Fixed)

Creates a new Fixed instance by dividing left by right.

[Pure]
public static Fixed operator /(Fixed left, Fixed right)

Parameters

left Fixed

First operand.

right Fixed

Second operand.

Returns

Fixed

New Fixed instance.

Exceptions

DivideByZeroException

When right is equal to 0.

operator ==(Fixed, Fixed)

Checks if left is equal to right.

[Pure]
public static bool operator ==(Fixed left, Fixed right)

Parameters

left Fixed

First operand.

right Fixed

Second operand.

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

f Fixed

Number to convert.

Returns

double

New double instance.

explicit operator long(Fixed)

Converts the provided f to long through truncation.

[Pure]
public static explicit operator long(Fixed f)

Parameters

f Fixed

Number to convert.

Returns

long

New long instance.

operator >(Fixed, Fixed)

Checks if left is greater than right.

[Pure]
public static bool operator >(Fixed left, Fixed right)

Parameters

left Fixed

First operand.

right Fixed

Second operand.

Returns

bool

true when left is greater than right, 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

left Fixed

First operand.

right Fixed

Second operand.

Returns

bool

true when left is greater than or equal to right, otherwise false.

implicit operator decimal(Fixed)

Converts the provided f to decimal.

[Pure]
public static implicit operator decimal(Fixed f)

Parameters

f Fixed

Number to convert.

Returns

decimal

New decimal instance.

operator ++(Fixed)

Creates a new Fixed instance by incrementing the provided f.

[Pure]
public static Fixed operator ++(Fixed f)

Parameters

f Fixed

Operand.

Returns

Fixed

New Fixed instance.

operator !=(Fixed, Fixed)

Checks if left is not equal to right.

[Pure]
public static bool operator !=(Fixed left, Fixed right)

Parameters

left Fixed

First operand.

right Fixed

Second operand.

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

left Fixed

First operand.

right Fixed

Second operand.

Returns

bool

true when left is less than right, 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

left Fixed

First operand.

right Fixed

Second operand.

Returns

bool

true when left is less than or equal to right, 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

left Fixed

First operand.

right Fixed

Second operand.

Returns

Fixed

New Fixed instance.

Exceptions

DivideByZeroException

When right is 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

left Fixed

First operand.

right Fixed

Second operand.

Returns

Fixed

New Fixed instance.

operator *(Fixed, Percent)

Creates a new Fixed instance by multiplying left and right together.

[Pure]
public static Fixed operator *(Fixed left, Percent right)

Parameters

left Fixed

First operand.

right Percent

Second operand.

Returns

Fixed

New Fixed instance.

operator *(Percent, Fixed)

Creates a new Fixed instance by multiplying left and right together.

[Pure]
public static Fixed operator *(Percent left, Fixed right)

Parameters

left Percent

First operand.

right Fixed

Second operand.

Returns

Fixed

New Fixed instance.

operator -(Fixed, Fixed)

Creates a new Fixed instance by subtracting right from left.

[Pure]
public static Fixed operator -(Fixed left, Fixed right)

Parameters

left Fixed

First operand.

right Fixed

Second operand.

Returns

Fixed

New Fixed instance.

operator -(Fixed)

Creates a new Fixed instance by negating the provided f.

[Pure]
public static Fixed operator -(Fixed f)

Parameters

f Fixed

Operand.

Returns

Fixed

New Fixed instance.