This is simple .NET Core library for parsing and working with fractinal expressions.
Run dotnet build
to build the project.
From Fractional.Tests directory, run dotnet test
to execute the unit tests.
This is a basic guide to show you the main features of the library.
Basic conversion operations
Conversion of decimals with approximate guessing :
var f1 = new Fractional(0.333333d, keepExcat: false);
var f2 = new Fractional(0.75d, keepExcat: false);
Console.WriteLine(f1.HumanRepresentation); // Output: 1/3
Console.WriteLine(f2.HumanRepresentation); // Output: 3/4
Conversion of decimals without approximate guessing :
var f1 = new Fractional(0.333333d, keepExcat: true);
var f2 = new Fractional(0.75d, keepExcat: true);
Console.WriteLine(f1); // Output: 333333/1000000
Console.WriteLine(f2); // Output: 75/100
Conversion to human fractions to decimals
var f1 = new Fractional("7 3/4");
var f2 = new Fractional("3/4");
Console.WriteLine(f1.Value); // Output: 7.75
Console.WriteLine(f2.Value); // Output: 0.75
The fraction instance work very well with math operators even with other numerics and string representations :
var f1 = new Fractional("3/4");
var f2 = new Fractional("1/2");
Console.WriteLine(f1 + 0.5d); // Output: 1 1/4
Console.WriteLine(f1 + f2); // Output: 1 1/4
Console.WriteLine(f1 - 1.75d); // Output: -1
Console.WriteLine(f1 - f2); // Output: 1/4
It does the same work with Comparisons operators
var f1 = new Fractional("5 3/2");
var f2 = new Fractional("3 3/2");
Console.WriteLine(f1 > f2); // Output: true
Console.WriteLine(f1 < 7.0d); // Output: true
Console.WriteLine(f2 > 5.0d); // Output: false