Table of Contents

Struct Percent

Namespace
LfrlAnvil.Numerics
Assembly
LfrlAnvil.Core.dll

Represents a lightweight percentage.

public readonly struct Percent : IEquatable<Percent>, IComparable<Percent>, IComparable, IFormattable
Implements
Inherited Members
Extension Methods

Constructors

Percent(decimal)

Creates a new Percent instance.

public Percent(decimal ratio)

Parameters

ratio decimal

Percentage ratio. 1 is equivalent to 100%.

Fields

One

Represents 1%.

public static readonly Percent One

Field Value

Percent

OneHundred

Represents 100%.

public static readonly Percent OneHundred

Field Value

Percent

Zero

Represents 0%.

public static readonly Percent Zero

Field Value

Percent

Properties

Ratio

Percentage ratio. 1 is equivalent to 100%.

public decimal Ratio { get; }

Property Value

decimal

Value

Percentage value. 100 is equivalent to 100%.

public decimal Value { get; }

Property Value

decimal

Methods

Abs()

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

[Pure]
public Percent Abs()

Returns

Percent

New Percent instance.

Add(Percent)

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

[Pure]
public Percent Add(Percent other)

Parameters

other Percent

Other instance to add.

Returns

Percent

New Percent instance.

Ceiling()

Creates a new Percent instance by calculating the ceiling of the Value from this instance.

[Pure]
public Percent Ceiling()

Returns

Percent

New Percent instance.

Remarks

See Ceiling(decimal) for more information.

CompareTo(Percent)

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(Percent other)

Parameters

other Percent

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)

Creates a new Percent instance.

[Pure]
public static Percent Create(decimal ratio)

Parameters

ratio decimal

Percentage ratio. 1 is equivalent to 100%.

Returns

Percent

Decrement()

Creates a new Percent instance by subtracting 1% from this instance.

[Pure]
public Percent Decrement()

Returns

Percent

New Percent instance.

Divide(Percent)

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

[Pure]
public Percent Divide(Percent other)

Parameters

other Percent

Other instance to divide by.

Returns

Percent

New Percent instance.

Exceptions

DivideByZeroException

When other is equal to 0%.

Equals(Percent)

Indicates whether the current object is equal to another object of the same type.

[Pure]
public bool Equals(Percent other)

Parameters

other Percent

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 Percent instance by calculating the floor of the Value from this instance.

[Pure]
public Percent Floor()

Returns

Percent

New Percent instance.

Remarks

See Floor(decimal) for more information.

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.

Increment()

Creates a new Percent instance by adding 1% to this instance.

[Pure]
public Percent Increment()

Returns

Percent

New Percent instance.

Modulo(Percent)

Creates a new Percent instance by calculating the remainder of division of this instance by other.

[Pure]
public Percent Modulo(Percent other)

Parameters

other Percent

Other instance to divide by.

Returns

Percent

New Percent instance.

Exceptions

DivideByZeroException

When other is equal to 0%.

Multiply(Percent)

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

[Pure]
public Percent Multiply(Percent other)

Parameters

other Percent

Other instance to multiply by.

Returns

Percent

New Percent instance.

Negate()

Creates a new Percent instance by negating this instance.

[Pure]
public Percent Negate()

Returns

Percent

New Percent instance.

Normalize(decimal)

Creates a new Percent instance from a decimal value.

[Pure]
public static Percent Normalize(decimal value)

Parameters

value decimal

Percentage value. 100 is equivalent to 100%.

Returns

Percent

Normalize(double)

Creates a new Percent instance from a double value.

[Pure]
public static Percent Normalize(double value)

Parameters

value double

Percentage value. 100 is equivalent to 100%.

Returns

Percent

Normalize(long)

Creates a new Percent instance from an long value.

[Pure]
public static Percent Normalize(long value)

Parameters

value long

Percentage value. 100 is equivalent to 100%.

Returns

Percent

Round(int, MidpointRounding)

Creates a new Percent instance by rounding the Value from this instance.

[Pure]
public Percent Round(int decimals, MidpointRounding mode = MidpointRounding.AwayFromZero)

Parameters

decimals int

Number of decimal places to round to.

mode MidpointRounding

Optional rounding strategy. Equal to AwayFromZero by default.

Returns

Percent

New Percent instance.

Remarks

See Round(decimal, MidpointRounding) for more information.

Subtract(Percent)

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

[Pure]
public Percent Subtract(Percent other)

Parameters

other Percent

Other instance to subtract.

Returns

Percent

New Percent instance.

ToString()

Returns a string representation of this Percent instance.

[Pure]
public override string ToString()

Returns

string

String representation.

ToString(IFormatProvider?)

Returns a string representation of this Percent 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 Percent 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 Percent instance by truncating the Value of this instance.

[Pure]
public Percent Truncate()

Returns

Percent

New Percent instance.

Remarks

See Truncate(decimal) for more information.

Operators

operator +(Percent, Percent)

Creates a new Percent instance by adding left and right together.

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

Parameters

left Percent

First operand.

right Percent

Second operand.

Returns

Percent

New Percent instance.

operator --(Percent)

Creates a new Percent instance by decrementing the provided percent.

[Pure]
public static Percent operator --(Percent percent)

Parameters

percent Percent

Operand.

Returns

Percent

New Percent instance.

operator /(Percent, Percent)

Creates a new Percent instance by dividing left by right.

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

Parameters

left Percent

First operand.

right Percent

Second operand.

Returns

Percent

New Percent instance.

Exceptions

DivideByZeroException

When right is equal to 0%.

operator ==(Percent, Percent)

Checks if left is equal to right.

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

Parameters

left Percent

First operand.

right Percent

Second operand.

Returns

bool

true when operands are equal, otherwise false.

explicit operator double(Percent)

Converts the provided percent to double. Returns Ratio.

[Pure]
public static explicit operator double(Percent percent)

Parameters

percent Percent

Percent to convert.

Returns

double

Ratio from the provided percent.

operator >(Percent, Percent)

Checks if left is greater than right.

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

Parameters

left Percent

First operand.

right Percent

Second operand.

Returns

bool

true when left is greater than right, otherwise false.

operator >=(Percent, Percent)

Checks if left is greater than or equal to right.

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

Parameters

left Percent

First operand.

right Percent

Second operand.

Returns

bool

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

implicit operator decimal(Percent)

Converts the provided percent to decimal. Returns Ratio.

[Pure]
public static implicit operator decimal(Percent percent)

Parameters

percent Percent

Percent to convert.

Returns

decimal

Ratio from the provided percent.

operator ++(Percent)

Creates a new Percent instance by incrementing the provided percent.

[Pure]
public static Percent operator ++(Percent percent)

Parameters

percent Percent

Operand.

Returns

Percent

New Percent instance.

operator !=(Percent, Percent)

Checks if left is not equal to right.

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

Parameters

left Percent

First operand.

right Percent

Second operand.

Returns

bool

true when operands are not equal, otherwise false.

operator <(Percent, Percent)

Checks if left is less than right.

[Pure]
public static bool operator <(Percent left, Percent right)

Parameters

left Percent

First operand.

right Percent

Second operand.

Returns

bool

true when left is less than right, otherwise false.

operator <=(Percent, Percent)

Checks if left is less than or equal to right.

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

Parameters

left Percent

First operand.

right Percent

Second operand.

Returns

bool

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

operator %(Percent, Percent)

Creates a new Percent instance by calculating the remainder of division of left by right.

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

Parameters

left Percent

First operand.

right Percent

Second operand.

Returns

Percent

New Percent instance.

Exceptions

DivideByZeroException

When right is equal to 0%.

operator *(Percent, Percent)

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

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

Parameters

left Percent

First operand.

right Percent

Second operand.

Returns

Percent

New Percent instance.

operator *(Percent, decimal)

Creates a new decimal instance by multiplying Ratio of left and right together.

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

Parameters

left Percent

First operand.

right decimal

Second operand.

Returns

decimal

New decimal instance.

operator *(Percent, double)

Creates a new double instance by multiplying Ratio of left and right together.

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

Parameters

left Percent

First operand.

right double

Second operand.

Returns

double

New double instance.

operator *(Percent, long)

Creates a new long instance by multiplying Ratio of left and right together and rounding the result using the AwayFromZero strategy.

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

Parameters

left Percent

First operand.

right long

Second operand.

Returns

long

New long instance.

operator *(Percent, float)

Creates a new float instance by multiplying Ratio of left and right together.

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

Parameters

left Percent

First operand.

right float

Second operand.

Returns

float

New float instance.

operator *(Percent, TimeSpan)

Creates a new TimeSpan instance by multiplying Ratio of left and right together and rounding the resulting Ticks using the AwayFromZero strategy.

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

Parameters

left Percent

First operand.

right TimeSpan

Second operand.

Returns

TimeSpan

New TimeSpan instance.

operator *(decimal, Percent)

Creates a new decimal instance by multiplying left and Ratio of right together.

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

Parameters

left decimal

First operand.

right Percent

Second operand.

Returns

decimal

New decimal instance.

operator *(double, Percent)

Creates a new double instance by multiplying left and Ratio of right together.

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

Parameters

left double

First operand.

right Percent

Second operand.

Returns

double

New double instance.

operator *(long, Percent)

Creates a new long instance by multiplying left and Ratio of right together and rounding the result using the AwayFromZero strategy.

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

Parameters

left long

First operand.

right Percent

Second operand.

Returns

long

New long instance.

operator *(float, Percent)

Creates a new float instance by multiplying left and Ratio of right together.

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

Parameters

left float

First operand.

right Percent

Second operand.

Returns

float

New float instance.

operator *(TimeSpan, Percent)

Creates a new TimeSpan instance by multiplying left and Ratio of right together and rounding the resulting Ticks using the AwayFromZero strategy.

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

Parameters

left TimeSpan

First operand.

right Percent

Second operand.

Returns

TimeSpan

New TimeSpan instance.

operator -(Percent, Percent)

Creates a new Percent instance by subtracting right from left.

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

Parameters

left Percent

First operand.

right Percent

Second operand.

Returns

Percent

New Percent instance.

operator -(Percent)

Creates a new Percent instance by negating the provided percent.

[Pure]
public static Percent operator -(Percent percent)

Parameters

percent Percent

Operand.

Returns

Percent

New Percent instance.