![]() |
VOOZH | about |
dotnet add package Fractions --version 7.5.0
NuGet\Install-Package Fractions -Version 7.5.0
<PackageReference Include="Fractions" Version="7.5.0" />
<PackageVersion Include="Fractions" Version="7.5.0" />Directory.Packages.props
<PackageReference Include="Fractions" />Project file
paket add Fractions --version 7.5.0
#r "nuget: Fractions, 7.5.0"
#:package Fractions@7.5.0
#addin nuget:?package=Fractions&version=7.5.0Install as a Cake Addin
#tool nuget:?package=Fractions&version=7.5.0Install as a Cake Tool
This package contains a data type to calculate with rational numbers. It supports basic mathematic operators such as:
The fraction data type implements operator overloads and implicit type conversion for convenience.
You can implicitly cast int, uint, long, ulong, decimal or BigInteger to Fraction:
Fraction a = 3; // int
Fraction b = 4L; // long
Fraction c = 3.3m; // decimal
Fraction d = new BigInteger(3);
// ..
You can explicitly cast double to Fraction:
var a = (Fraction)3.3; // double
You can explicitly cast from Fraction to any supported data type (int, uint, long, ulong, BigInteger, decimal, double). However, be aware that an OverflowException will be thrown, if the target data type's boundary values are exceeded.
There a three types of constructors available:
new Fraction (<value>) for int, uint, long, ulong, BigInteger, decimal and double.new Fraction (<numerator>, <denominator>) using BigInteger for numerator and denominator.new Fraction (<numerator>, <denominator>, <reduce>) using BigInteger for numerator and denominator + bool to indicate if the resulting fraction shall be normalized (reduced).Fraction.FromDecimal(decimal)Fraction.FromDouble(double)Fraction.FromDoubleRounded(double)Fraction.FromString(string) (using current culture)Fraction.FromString(string, IFormatProvider)Fraction.FromString(string, NumberStyles, IFormatProvider)Fraction.TryParse(string, out Fraction) (using current culture)Fraction.TryParse(string, NumberStyles, IFormatProvider, out Fraction)Fraction.TryParse(ReadOnlySpan<char>, NumberStyles, IFormatProvider, bool, out Fraction)doubleThe double data type stores its values as 64bit floating point numbers that comply with IEC 60559:1989 (IEEE 754) standard for binary floating-point arithmetic. double cannot store some binary fractions. For example, 1/10, which is represented precisely by .1 as a decimal fraction, is represented by .0001100110011... as a binary fraction, with the pattern 0011 repeating to infinity. In this case, the floating-point value provides an imprecise representation of the number that it represents:
var value = Fraction.FromDouble(0.1);
/* Returns 3602879701896397/36028797018963968
* which is 0.10000000000000000555111512312578 */
Console.WriteLine(value);
You can use the Fraction.FromDoubleRounded(double) method to avoid big numbers in numerator and denominator. But please keep in mind that the creation speed is significantly slower than using the pure value from Fraction.FromDouble(double). Example:
var value = Fraction.FromDoubleRounded(0.1);
// Returns 1/10 which is 0.1
Console.WriteLine(value);
stringThe following string patterns can be parsed:
[+/-]n where n is an integer. Examples: +5, -6, 1234, 0[+/-]n.m where n and m are integers. The decimal point symbol depends on the system's culture settings. Examples: -4.3, 0.45[+/-]n/[+/-]m where n and m are integers. Examples: 1/2, -4/5, +4/-3, 32/100
Example:var value = Fraction.FromString("1,5", new CultureInfo("de-DE"))
// Returns 3/2 which is 1.5
Console.WriteLine(value);
You should consider the TryParse methods when reading numbers as text from user input. Furthermore it is best practice to always supply a culture information (e.g. CultureInfo.InvariantCulture). Otherwise you will sooner or later parse wrong numbers because of different decimal point symbols or included Thousands character.
You can convert a Fraction to any supported data type by calling:
.ToInt32().ToUInt32().ToInt64().ToUInt64().ToBigInteger().ToDecimal().ToDouble().ToString() (using current culture).ToString(string) (using format string and the system's current culture).ToString(string,IFormatProvider)If the target's data type boundary values are exceeded the system will throw an OverflowException.
Example:
var rationalNumber = new Fraction(1, 3);
var value = rationalNumber.ToDecimal();
// result is 0.33333
Console.WriteLine(Math.Round(value, 5));
| Character | Description |
|---|---|
| G | General format: <numerator>/<denominator> e.g. 1/3 |
| n | Numerator |
| d | Denominator |
| z | The fraction as integer |
| r | The positive remainder of all digits after the decimal point using the format: <numerator>/<denominator> or string.Empty if the fraction is a valid integer without digits after the decimal point. |
| m | The fraction as mixed number e.g. 2 1/3 instead of 7/3 |
Note: The special characters #, and 0 like in #.### are not supported. Convert the Fraction to decimal if you want to display rounded decimal values.
Example:
var value = new Fraction(3, 2);
// returns 1 1/2
Console.WriteLine(value.ToString("m", new CultureInfo("de-DE")));
The following mathematic operations are supported:
.Reduce() returns a normalized fraction (e.g. 2/4 → 1/2).Add(Fraction) returns the sum of (a + b).Subtract(Fraction) returns the difference of (a - b).Multiply(Fraction) returns the product of (a * b).Divide(Fraction) returns the quotient of (a / b).Remainder(Fraction) returns the remainder (or left over) of (a % b).Invert() returns an inverted fraction (same operation as (a * -1)).Abs() returns the absolute value |a|Fraction.Pow(Fraction, int) returns a base raised to a power (a ^ exponent) (e.g. 1/10^(-1) → 10/1)Fraction.Round(Fraction, int, MidpointRounding) returns the fraction, which is rounded to the specified precisionFraction.RoundToBigInteger(Fraction, MidpointRounding) returns the fraction as rounded BigIntegerAs extension method:
FractionExt.Sqrt(this Fraction, int) returns the square root, specifying the precision after the decimal point.Example:
var a = new Fraction(1, 3);
var b = new Fraction(2, 3);
var result = a * b;
// returns 2/9 which is 0,2222...
Console.WriteLine(result);
Fraction implements the following interfaces:
IEquatable<Fraction>,IComparable,IComparable<Fraction>Please note that .Equals(Fraction) will compare the exact values of numerator and denominator. That said:
var a = new Fraction(1, 2, true);
var b = new Fraction(1, 2, false);
var c = new Fraction(2, 4, false);
// result1 is true
var result1 = a == a;
// result2 is true
var result2 = a == b;
// result3 is false
var result3 = a == c;
You have to use .IsEquivalentTo(Fraction) if want to test non-normalized fractions for value-equality.
The data type stores the numerator and denominator as BigInteger. Per default it will reduce fractions to its normalized form during creation. The result of each mathematical operation will be reduced as well. There is a special constructor to create a non-normalized fraction. Be aware that Equals relies on normalized values when comparing two different instances.
Just run dotnet build -c release.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 is compatible. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on Fractions:
| Package | Downloads |
|---|---|
|
KubernetesClient
Client library for the Kubernetes open source container orchestrator. |
|
|
KubernetesClient.Models
Client library for the Kubernetes open source container orchestrator. |
|
|
EngineeringUnits
An Engineering version of UnitsNet - Do calculations and converts between any type of unit |
|
|
KubernetesClient.Classic
Client library for the Kubernetes open source container orchestrator. |
|
|
KubernetesClient.Aot
Client library for the Kubernetes open source container orchestrator. |
Showing the top 3 popular GitHub repositories that depend on Fractions:
| Repository | Stars |
|---|---|
|
kubernetes-client/csharp
Officially supported dotnet Kubernetes Client library
|
|
|
AngryCarrot789/FramePFX
A non-linear video editor written in C# using Avalonia
|
|
|
JayArrowz/PancakeTokenSniper
BSC BNB Pancake token sniper, buy, take profit and rug check
|
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 8.3.2 | 447,548 | 3/30/2025 | |
| 8.3.1 | 8,103 | 3/12/2025 | |
| 8.3.0 | 7,660 | 2/23/2025 | |
| 8.2.0 | 3,562 | 2/16/2025 | |
| 8.1.1 | 13,109 | 12/10/2024 | |
| 8.1.0 | 5,147 | 11/20/2024 | |
| 8.0.4 | 98,564 | 8/19/2024 | |
| 8.0.3 | 124,180 | 7/22/2024 | |
| 8.0.2 | 5,564 | 7/4/2024 | |
| 8.0.1 | 2,447 | 7/3/2024 | 8.0.1 is deprecated because it has critical bugs. |
| 8.0.0 | 5,516 | 7/3/2024 | 8.0.0 is deprecated because it has critical bugs. |
| 7.7.1 | 43,177 | 5/6/2024 | |
| 7.7.0 | 7,778 | 4/17/2024 | |
| 7.6.1 | 3,485 | 4/15/2024 | |
| 7.6.0 | 3,225 | 4/13/2024 | |
| 7.5.0 | 2,441 | 4/12/2024 | |
| 7.4.1 | 58,215 | 3/31/2024 | |
| 7.4.0 | 5,794 | 3/31/2024 | |
| 7.3.0 | 49,304,113 | 10/1/2023 | |
| 7.2.1 | 24,785,793 | 12/11/2022 |