Contains extension methods for converting decimals to common fractions.
public static class FractionalizeExtensionsInheritance System.Object → FractionalizeExtensions
Converts a decimal to the closest reduced fraction whose denominator does not exceed the specified maximum.
public static string Fractionalize(this decimal input, int maxDenominator, decimal tolerance, bool useUnicode=false);input System.Decimal
The decimal to convert.
maxDenominator System.Int32
The largest denominator that may be used.
tolerance System.Decimal
The maximum absolute difference allowed between the input and the fraction.
useUnicode System.Boolean
Whether to use a Unicode vulgar-fraction character when an exact character exists.
System.String
A whole number, proper fraction, or mixed fraction when the closest fraction is within
tolerance; otherwise, the culture-formatted input.
System.ArgumentOutOfRangeException
Thrown when maxDenominator is less than one or tolerance is negative.
1.25m.Fractionalize(5, 0m) => "1 1/4"
0.34m.Fractionalize(5, 0.01m) => "1/3"
0.75m.Fractionalize(4, 0m, useUnicode: true) => "¾"The tolerance boundary is inclusive. Equidistant fractions prefer the smaller denominator, then the value farther from zero. Fraction components use invariant digits; slash notation is used unless useUnicode requests an available exact glyph.