Humanizer.FractionalizeExtensions
FractionalizeExtensions Class
Contains extension methods for converting decimals to common fractions.
public static class FractionalizeExtensions
Inheritance System.Object → FractionalizeExtensions
Methods
FractionalizeExtensions.Fractionalize(this decimal, int, decimal, bool) Method
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);
Parameters
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.
Returns
System.String
A whole number, proper fraction, or mixed fraction when the closest fraction is within
tolerance; otherwise, the culture-formatted input.
Exceptions
System.ArgumentOutOfRangeException
Thrown when maxDenominator is less than one or tolerance is negative.
Example
1.25m.Fractionalize(5, 0m) => "1 1/4"
0.34m.Fractionalize(5, 0.01m) => "1/3"
0.75m.Fractionalize(4, 0m, useUnicode: true) => "¾"
Remarks
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.