Struct Percent
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
decimalPercentage ratio. 1 is equivalent to 100%.
Fields
One
Represents 1%.
public static readonly Percent One
Field Value
OneHundred
Represents 100%.
public static readonly Percent OneHundred
Field Value
Zero
Represents 0%.
public static readonly Percent Zero
Field Value
Properties
Ratio
Percentage ratio. 1 is equivalent to 100%.
public decimal Ratio { get; }
Property Value
Value
Percentage value. 100 is equivalent to 100%.
public decimal Value { get; }
Property Value
Methods
Abs()
Creates a new Percent instance by calculating an absolute value from this instance.
[Pure]
public Percent Abs()
Returns
Add(Percent)
Creates a new Percent instance by adding other
to this instance.
[Pure]
public Percent Add(Percent other)
Parameters
other
PercentOther instance to add.
Returns
Ceiling()
[Pure]
public Percent Ceiling()
Returns
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
PercentAn 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
objectAn 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
decimalPercentage ratio. 1 is equivalent to 100%.
Returns
Decrement()
Creates a new Percent instance by subtracting 1% from this instance.
[Pure]
public Percent Decrement()
Returns
Divide(Percent)
Creates a new Percent instance by dividing this instance by other
.
[Pure]
public Percent Divide(Percent other)
Parameters
other
PercentOther instance to divide by.
Returns
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
PercentAn 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
obj
objectThe 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()
[Pure]
public Percent Floor()
Returns
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
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
PercentOther instance to divide by.
Returns
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
PercentOther instance to multiply by.
Returns
Negate()
Creates a new Percent instance by negating this instance.
[Pure]
public Percent Negate()
Returns
Normalize(decimal)
[Pure]
public static Percent Normalize(decimal value)
Parameters
value
decimalPercentage value. 100 is equivalent to 100%.
Returns
Normalize(double)
[Pure]
public static Percent Normalize(double value)
Parameters
value
doublePercentage value. 100 is equivalent to 100%.
Returns
Normalize(long)
[Pure]
public static Percent Normalize(long value)
Parameters
value
longPercentage value. 100 is equivalent to 100%.
Returns
Round(int, MidpointRounding)
[Pure]
public Percent Round(int decimals, MidpointRounding mode = MidpointRounding.AwayFromZero)
Parameters
decimals
intNumber of decimal places to round to.
mode
MidpointRoundingOptional rounding strategy. Equal to AwayFromZero by default.
Returns
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
PercentOther instance to subtract.
Returns
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
IFormatProviderAn 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
stringAn optional numeric format.
formatProvider
IFormatProviderAn optional format provider.
Returns
- string
String representation.
Truncate()
[Pure]
public Percent Truncate()
Returns
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
Returns
operator --(Percent)
Creates a new Percent instance by decrementing the provided percent
.
[Pure]
public static Percent operator --(Percent percent)
Parameters
percent
PercentOperand.
Returns
operator /(Percent, Percent)
Creates a new Percent instance by dividing left
by right
.
[Pure]
public static Percent operator /(Percent left, Percent right)
Parameters
Returns
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
Returns
- bool
true when operands are equal, otherwise false.
explicit operator double(Percent)
[Pure]
public static explicit operator double(Percent percent)
Parameters
percent
PercentPercent to convert.
Returns
operator >(Percent, Percent)
Checks if left
is greater than right
.
[Pure]
public static bool operator >(Percent left, Percent right)
Parameters
Returns
- bool
true when
left
is greater thanright
, 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
Returns
- bool
true when
left
is greater than or equal toright
, otherwise false.
implicit operator decimal(Percent)
[Pure]
public static implicit operator decimal(Percent percent)
Parameters
percent
PercentPercent to convert.
Returns
operator ++(Percent)
Creates a new Percent instance by incrementing the provided percent
.
[Pure]
public static Percent operator ++(Percent percent)
Parameters
percent
PercentOperand.
Returns
operator !=(Percent, Percent)
Checks if left
is not equal to right
.
[Pure]
public static bool operator !=(Percent left, Percent right)
Parameters
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
Returns
- bool
true when
left
is less thanright
, 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
Returns
- bool
true when
left
is less than or equal toright
, 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
Returns
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
Returns
operator *(Percent, decimal)
[Pure]
public static decimal operator *(Percent left, decimal right)
Parameters
Returns
operator *(Percent, double)
[Pure]
public static double operator *(Percent left, double right)
Parameters
Returns
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
Returns
operator *(Percent, float)
[Pure]
public static float operator *(Percent left, float right)
Parameters
Returns
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
Returns
operator *(decimal, Percent)
[Pure]
public static decimal operator *(decimal left, Percent right)
Parameters
Returns
operator *(double, Percent)
[Pure]
public static double operator *(double left, Percent right)
Parameters
Returns
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
Returns
operator *(float, Percent)
[Pure]
public static float operator *(float left, Percent right)
Parameters
Returns
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
Returns
operator -(Percent, Percent)
Creates a new Percent instance by subtracting right
from left
.
[Pure]
public static Percent operator -(Percent left, Percent right)
Parameters
Returns
operator -(Percent)
Creates a new Percent instance by negating the provided percent
.
[Pure]
public static Percent operator -(Percent percent)
Parameters
percent
PercentOperand.