Skip to content

OnixLabs.Numerics.NumberInfo

matthew edited this page May 27, 2026 · 2 revisions

NumberInfo Struct

Represents component information about a rational number, decomposing it into its constituent parts: unscaled value, scale, significand, exponent, sign, and precision.

Namespace OnixLabs.Numerics
Assembly OnixLabs.Numerics.dll (NuGet)
Inheritance object → ValueType → NumberInfo
Implements IValueEquatable<NumberInfo>, IValueComparable<NumberInfo>, ISpanParsable<NumberInfo>, IFormattable

Definition

public readonly partial struct NumberInfo :
    IValueEquatable<NumberInfo>,
    IValueComparable<NumberInfo>,
    ISpanParsable<NumberInfo>,
    IFormattable

Remarks

NumberInfo captures the internal representation of a rational number as a pair of values: an unscaled value (a BigInteger that includes all significant digits and any trailing zeros) and a scale (a non-negative integer indicating how many digits from the right of the unscaled value form the fractional part). From these two values all other properties — Significand, Exponent, Precision, and Sign — are derived on demand.

NumberInfo is the underlying representation used by BigDecimal. The BigDecimal.ToNumberInfo() method exposes this representation, and every numeric type in OnixLabs.Numerics provides a ToNumberInfo() extension method via NumericsExtensions.

Strict vs Semantic equality

NumberInfo distinguishes two equality modes:

  • Strict equality — two values are equal only when their UnscaledValue and Scale are both identical. For example, 1.20 and 1.2 are not strictly equal because their unscaled values differ (120 vs 12). This is the mode used by Equals(NumberInfo), Equals(object?), and GetHashCode().
  • Semantic equality — two values are equal when they represent the same mathematical quantity, regardless of trailing zeros or scale. 1.20 and 1.2 are semantically equal. This is the mode used by the == and != operators.

Both modes are exposed through the NumberInfoEqualityComparer type.

Note The static Equals(NumberInfo, NumberInfo) overload and the Equals(NumberInfo, NumberInfoEqualityComparer) family allow callers to choose the comparer explicitly.

Examples

Inspecting component parts

using OnixLabs.Numerics;

NumberInfo info = NumberInfo.Parse("123.450");

Console.WriteLine(info.UnscaledValue); // 123450
Console.WriteLine(info.Scale);         // 3
Console.WriteLine(info.Significand);   // 12345
Console.WriteLine(info.Exponent);      // -2
Console.WriteLine(info.Precision);     // 6
Console.WriteLine(info.Sign);          // 1

Strict vs semantic equality

using OnixLabs.Numerics;

NumberInfo a = NumberInfo.Parse("1.20");
NumberInfo b = NumberInfo.Parse("1.2");

// Semantic equality: same numeric value, different scale
Console.WriteLine(a == b);                                                       // True
Console.WriteLine(NumberInfo.Equals(a, b, NumberInfoEqualityComparer.Semantic)); // True

// Strict equality: scale must also match
Console.WriteLine(a.Equals(b));                                                  // False
Console.WriteLine(NumberInfo.Equals(a, b, NumberInfoEqualityComparer.Strict));   // False

Static Properties

Property Type Description
Zero NumberInfo Gets a NumberInfo instance representing zero.
One NumberInfo Gets a NumberInfo instance representing one.
NegativeOne NumberInfo Gets a NumberInfo instance representing negative one.

Zero

public static NumberInfo Zero { get; }

Gets a NumberInfo instance representing zero (UnscaledValue = 0, Scale = 0).

One

public static NumberInfo One { get; }

Gets a NumberInfo instance representing one (UnscaledValue = 1, Scale = 0).

NegativeOne

public static NumberInfo NegativeOne { get; }

Gets a NumberInfo instance representing negative one (UnscaledValue = -1, Scale = 0).

Properties

Property Type Description
UnscaledValue BigInteger Gets the unscaled value of the represented number, including any trailing zeros.
Scale int Gets the scale of the represented number.
Significand BigInteger Gets the significand of the represented number, with trailing zeros stripped.
Exponent int Gets the exponent of the represented number.
Precision int Gets the total number of significant digits in the represented number.
Sign int Gets the sign of the represented number.

UnscaledValue

public BigInteger UnscaledValue { get; }

Gets the unscaled value of the represented number. The unscaled value is a signed BigInteger that includes any trailing zeros. When the represented number has a fractional component, trailing zeros in the unscaled value are considered significant — for example, 1.20 has UnscaledValue = 120 and Scale = 2.

Scale

public int Scale { get; }

Gets the scale of the represented number. The scale is a non-negative integer indicating how many digits from the right-hand side of UnscaledValue form the fractional component of the number. A scale of 0 means the number is an integer. For example, 123.45 has Scale = 2.

Significand

public BigInteger Significand { get; }

Gets the significand of the represented number. The significand is the signed UnscaledValue with all trailing zeros stripped. For example, 123.450 has UnscaledValue = 123450 and Significand = 12345. When UnscaledValue is zero, Significand is also zero.

Exponent

public int Exponent { get; }

Gets the exponent of the represented number, expressed as a positive, negative, or zero integer. The exponent reflects the power of ten required to recover the original value from the Significand. For example, 123.450 has Significand = 12345 and Exponent = -2, because 12345 × 10⁻² = 123.45. When UnscaledValue is zero, Exponent is 0.

Precision

public int Precision { get; }

Gets the total number of significant digits in the represented number. If UnscaledValue or Significand contain fewer digits than Precision, trailing zeros are considered significant. For example, 1.200 has Precision = 4.

Sign

public int Sign { get; }

Gets the sign of the represented number. Returns -1 for negative numbers, 1 for positive numbers, and 0 for zero.

Methods

Static Methods

Method Returns Description
Compare(NumberInfo, NumberInfo) int Compares two NumberInfo values and returns an integer indicating their relative order.
Equals(NumberInfo, NumberInfo) bool Determines whether two NumberInfo values are equal using the Strict comparer.
Equals(NumberInfo, NumberInfo, NumberInfoEqualityComparer) bool Determines whether two NumberInfo values are equal using the specified comparer.
Parse(string, IFormatProvider?) NumberInfo Parses a string into a NumberInfo value.
Parse(string, NumberStyles, IFormatProvider?) NumberInfo Parses a string with the specified styles into a NumberInfo value.
Parse(ReadOnlySpan&lt;char&gt;, IFormatProvider?) NumberInfo Parses a ReadOnlySpan<char> into a NumberInfo value.
Parse(ReadOnlySpan&lt;char&gt;, NumberStyles, IFormatProvider?) NumberInfo Parses a ReadOnlySpan<char> with the specified styles into a NumberInfo value.
TryParse(string?, out NumberInfo) bool Tries to parse a string into a NumberInfo value.
TryParse(string?, IFormatProvider?, out NumberInfo) bool Tries to parse a string using the specified format provider.
TryParse(string?, NumberStyles, IFormatProvider?, out NumberInfo) bool Tries to parse a string with the specified styles.
TryParse(ReadOnlySpan&lt;char&gt;, out NumberInfo) bool Tries to parse a ReadOnlySpan<char> into a NumberInfo value.
TryParse(ReadOnlySpan&lt;char&gt;, IFormatProvider?, out NumberInfo) bool Tries to parse a ReadOnlySpan<char> using the specified format provider.
TryParse(ReadOnlySpan&lt;char&gt;, NumberStyles, IFormatProvider?, out NumberInfo) bool Tries to parse a ReadOnlySpan<char> with the specified styles.

Instance Methods

Method Returns Description
CompareTo(NumberInfo) int Compares this instance with another NumberInfo value.
CompareTo(object?) int Compares this instance with another object.
Equals(NumberInfo) bool Determines whether this instance equals another NumberInfo value using the Strict comparer.
Equals(NumberInfo, NumberInfoEqualityComparer) bool Determines whether this instance equals another NumberInfo value using the specified comparer.
Equals(object?) bool Determines whether this instance equals another object using the Strict comparer.
Equals(object?, NumberInfoEqualityComparer) bool Determines whether this instance equals another object using the specified comparer.
GetHashCode() int Returns a hash code for this instance using the Strict comparer.
ToString() string Returns a string representation using the default "G" format and the current culture.
ToString(string?, IFormatProvider?) string Returns a string representation using the specified format and format provider.
ToString(ReadOnlySpan&lt;char&gt;, IFormatProvider?) string Returns a string representation using the specified span format and format provider.

Compare(NumberInfo, NumberInfo)

public static int Compare(NumberInfo left, NumberInfo right)

Compares two NumberInfo values and returns an integer that indicates whether left is less than, equal to, or greater than right. Delegates to NumberInfoOrdinalityComparer.Default.

Parameter Type Description
left NumberInfo The left value to compare.
right NumberInfo The right value to compare.

Returns — A signed integer: negative if left precedes right, zero if they are equal, positive if left follows right.


Equals(NumberInfo, NumberInfo)

public static bool Equals(NumberInfo left, NumberInfo right)

Determines whether two NumberInfo values are equal using the Strict comparer. Both UnscaledValue and Scale must match.

Parameter Type Description
left NumberInfo The left value to compare.
right NumberInfo The right value to compare.

Returns — true if the two values are strictly equal; otherwise, false.


Equals(NumberInfo, NumberInfo, NumberInfoEqualityComparer)

public static bool Equals(NumberInfo left, NumberInfo right, NumberInfoEqualityComparer comparer)

Determines whether two NumberInfo values are equal using the specified comparer.

Parameter Type Description
left NumberInfo The left value to compare.
right NumberInfo The right value to compare.
comparer NumberInfoEqualityComparer The equality comparer to use.

Returns — true if the two values are equal according to comparer; otherwise, false.


Parse(string, IFormatProvider?)

public static NumberInfo Parse(string value, IFormatProvider? provider = null)

Parses the specified string into a NumberInfo value using NumberStyles.Any. When provider is null, the current culture is used.

Parameter Type Description
value string The string to parse.
provider IFormatProvider? An object that provides culture-specific formatting information, or null to use the current culture.

Returns — A new NumberInfo instance parsed from value.

Exceptions

Exception Condition
FormatException value is not in a recognised format.

Parse(string, NumberStyles, IFormatProvider?)

public static NumberInfo Parse(string value, NumberStyles style, IFormatProvider? provider = null)

Parses the specified string into a NumberInfo value using the given NumberStyles.

Parameter Type Description
value string The string to parse.
style NumberStyles A bitwise combination of styles that may appear in value.
provider IFormatProvider? An object that provides culture-specific formatting information, or null to use the current culture.

Returns — A new NumberInfo instance parsed from value.

Exceptions

Exception Condition
FormatException value is not in a recognised format.

Parse(ReadOnlySpan<char>, IFormatProvider?)

public static NumberInfo Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null)

Parses the specified ReadOnlySpan<char> into a NumberInfo value using NumberStyles.Any.

Parameter Type Description
value ReadOnlySpan<char> The character span to parse.
provider IFormatProvider? An object that provides culture-specific formatting information, or null to use the current culture.

Returns — A new NumberInfo instance parsed from value.

Exceptions

Exception Condition
FormatException value is not in a recognised format.

Parse(ReadOnlySpan<char>, NumberStyles, IFormatProvider?)

public static NumberInfo Parse(ReadOnlySpan<char> value, NumberStyles style, IFormatProvider? provider = null)

Parses the specified ReadOnlySpan<char> into a NumberInfo value using the given NumberStyles.

Parameter Type Description
value ReadOnlySpan<char> The character span to parse.
style NumberStyles A bitwise combination of styles that may appear in value.
provider IFormatProvider? An object that provides culture-specific formatting information, or null to use the current culture.

Returns — A new NumberInfo instance parsed from value.

Exceptions

Exception Condition
FormatException value is not in a recognised format.

TryParse(string?, out NumberInfo)

public static bool TryParse(string? value, out NumberInfo result)

Tries to parse the specified string into a NumberInfo value using NumberStyles.Any and the current culture.

Parameter Type Description
value string? The string to parse.
result out NumberInfo On return, contains the parsed value, or the default if parsing failed.

Returns — true if parsing succeeded; otherwise, false.


TryParse(string?, IFormatProvider?, out NumberInfo)

public static bool TryParse(string? value, IFormatProvider? provider, out NumberInfo result)

Tries to parse the specified string into a NumberInfo value using the given format provider and NumberStyles.Any.

Parameter Type Description
value string? The string to parse.
provider IFormatProvider? An object that provides culture-specific formatting information, or null to use the current culture.
result out NumberInfo On return, contains the parsed value, or the default if parsing failed.

Returns — true if parsing succeeded; otherwise, false.


TryParse(string?, NumberStyles, IFormatProvider?, out NumberInfo)

public static bool TryParse(string? value, NumberStyles style, IFormatProvider? provider, out NumberInfo result)

Tries to parse the specified string into a NumberInfo value using the given styles and format provider.

Parameter Type Description
value string? The string to parse.
style NumberStyles A bitwise combination of styles that may appear in value.
provider IFormatProvider? An object that provides culture-specific formatting information, or null to use the current culture.
result out NumberInfo On return, contains the parsed value, or the default if parsing failed.

Returns — true if parsing succeeded; otherwise, false.


TryParse(ReadOnlySpan<char>, out NumberInfo)

public static bool TryParse(ReadOnlySpan<char> value, out NumberInfo result)

Tries to parse the specified ReadOnlySpan<char> into a NumberInfo value using NumberStyles.Any and the current culture.

Parameter Type Description
value ReadOnlySpan<char> The character span to parse.
result out NumberInfo On return, contains the parsed value, or the default if parsing failed.

Returns — true if parsing succeeded; otherwise, false.


TryParse(ReadOnlySpan<char>, IFormatProvider?, out NumberInfo)

public static bool TryParse(ReadOnlySpan<char> value, IFormatProvider? provider, out NumberInfo result)

Tries to parse the specified ReadOnlySpan<char> into a NumberInfo value using the given format provider and NumberStyles.Any.

Parameter Type Description
value ReadOnlySpan<char> The character span to parse.
provider IFormatProvider? An object that provides culture-specific formatting information, or null to use the current culture.
result out NumberInfo On return, contains the parsed value, or the default if parsing failed.

Returns — true if parsing succeeded; otherwise, false.


TryParse(ReadOnlySpan<char>, NumberStyles, IFormatProvider?, out NumberInfo)

public static bool TryParse(ReadOnlySpan<char> value, NumberStyles style, IFormatProvider? provider, out NumberInfo result)

Tries to parse the specified ReadOnlySpan<char> into a NumberInfo value using the given styles and format provider.

Parameter Type Description
value ReadOnlySpan<char> The character span to parse.
style NumberStyles A bitwise combination of styles that may appear in value.
provider IFormatProvider? An object that provides culture-specific formatting information, or null to use the current culture.
result out NumberInfo On return, contains the parsed value, or the default if parsing failed.

Returns — true if parsing succeeded; otherwise, false.


CompareTo(NumberInfo)

public int CompareTo(NumberInfo other)

Compares this instance with another NumberInfo value. Delegates to NumberInfoOrdinalityComparer.Default.

Parameter Type Description
other NumberInfo The value to compare with this instance.

Returns — A signed integer indicating the relative order of this instance and other.


CompareTo(object?)

public int CompareTo(object? obj)

Compares this instance with another object. Delegates to NumberInfoOrdinalityComparer.Default.

Parameter Type Description
obj object? The object to compare with this instance.

Returns — A signed integer indicating the relative order of this instance and obj.


Equals(NumberInfo)

public bool Equals(NumberInfo other)

Determines whether this instance is equal to another NumberInfo value using the Strict comparer. Both UnscaledValue and Scale must match.

Parameter Type Description
other NumberInfo The value to compare with this instance.

Returns — true if this instance is strictly equal to other; otherwise, false.


Equals(NumberInfo, NumberInfoEqualityComparer)

public bool Equals(NumberInfo other, NumberInfoEqualityComparer comparer)

Determines whether this instance is equal to another NumberInfo value using the specified comparer.

Parameter Type Description
other NumberInfo The value to compare with this instance.
comparer NumberInfoEqualityComparer The equality comparer to use.

Returns — true if this instance is equal to other according to comparer; otherwise, false.


Equals(object?)

public override bool Equals(object? obj)

Determines whether this instance is equal to another object using the Strict comparer.

Parameter Type Description
obj object? The object to compare with this instance.

Returns — true if obj is a NumberInfo strictly equal to this instance; otherwise, false.


Equals(object?, NumberInfoEqualityComparer)

public bool Equals(object? obj, NumberInfoEqualityComparer comparer)

Determines whether this instance is equal to another object using the specified comparer.

Parameter Type Description
obj object? The object to compare with this instance.
comparer NumberInfoEqualityComparer The equality comparer to use.

Returns — true if obj is a NumberInfo equal to this instance according to comparer; otherwise, false.


GetHashCode()

public override int GetHashCode()

Returns a hash code for this instance using the Strict comparer. The hash combines UnscaledValue and Scale.

Returns — A 32-bit signed integer hash code.


ToString()

public override string ToString()

Returns a string representation of this instance using the default "G" format and CultureInfo.CurrentCulture.

Returns — The formatted string representation of this instance.


ToString(string?, IFormatProvider?)

public string ToString(string? format, IFormatProvider? formatProvider = null)

Returns a string representation of this instance using the specified format and format provider. When format is null, the default "G" format is used. When formatProvider is null, the current culture is used.

Supported format specifiers: C (currency), D (decimal), E (exponential/scientific), F (fixed-point), G (general), N (number), P (percent), R (round-trip), X (hexadecimal).

Parameter Type Description
format string? The format specifier, or null for the default "G" format.
formatProvider IFormatProvider? An object that provides culture-specific formatting information, or null to use the current culture.

Returns — The formatted string representation of this instance.


ToString(ReadOnlySpan<char>, IFormatProvider?)

public string ToString(ReadOnlySpan<char> format, IFormatProvider? formatProvider = null)

Returns a string representation of this instance using the specified span format and format provider.

Parameter Type Description
format ReadOnlySpan<char> The format specifier as a character span.
formatProvider IFormatProvider? An object that provides culture-specific formatting information, or null to use the current culture.

Returns — The formatted string representation of this instance.

Operators

Operator Description
operator == Determines whether two NumberInfo values are semantically equal.
operator != Determines whether two NumberInfo values are not semantically equal.
operator > Determines whether the left value is greater than the right value.
operator >= Determines whether the left value is greater than or equal to the right value.
operator < Determines whether the left value is less than the right value.
operator <= Determines whether the left value is less than or equal to the right value.

operator ==

public static bool operator ==(NumberInfo left, NumberInfo right)

Determines whether two NumberInfo values are semantically equal. Uses the Semantic comparer; 1.20 == 1.2 evaluates to true.

operator !=

public static bool operator !=(NumberInfo left, NumberInfo right)

Determines whether two NumberInfo values are not semantically equal. Uses the Semantic comparer; 1.20 != 1.200 evaluates to false.

operator >

public static bool operator >(NumberInfo left, NumberInfo right)

Determines whether left is greater than right. Delegates to NumberInfoOrdinalityComparer.Default.

operator >=

public static bool operator >=(NumberInfo left, NumberInfo right)

Determines whether left is greater than or equal to right. Delegates to NumberInfoOrdinalityComparer.Default.

operator <

public static bool operator <(NumberInfo left, NumberInfo right)

Determines whether left is less than right. Delegates to NumberInfoOrdinalityComparer.Default.

operator <=

public static bool operator <=(NumberInfo left, NumberInfo right)

Determines whether left is less than or equal to right. Delegates to NumberInfoOrdinalityComparer.Default.

See Also

ONIXLabs .NET Library


Clone this wiki locally