A package that helps you dealing with mathematical fractions.
Fractionwhich represents fractions in the form
MixedFractionwhich represents mixed fractions in the form
Working with fractions #
You can create an instance of
Fraction using one of its constructors.
Basic: it just requires the numerator and/or the denominator.
final frac = Fraction(3, 5); // 3/5 final frac = Fraction(3, 5); // 3/1
String: pass the fraction as a string but it has to be well-formed otherwise an exception is thrown.
final frac1 = Fraction.fromString("2/4"); // 2/4 final frac2 = Fraction.fromString("-2/4"); // -2/4 final frac3 = Fraction.fromString("2/-4"); // Error final frac4 = Fraction.fromString("-2"); // -2/1
double: represents a double as a fraction. Note that irrational numbers cannot be converted into a fraction by definition; the constructor has the
precisionparameter which decides how precise the representation has to be.
final frac1 = Fraction.fromDouble(1.5); // 3/2 final frac2 = Fraction.fromString(-8.5); // -17/2 final frac3 = Fraction.fromString(math.pi); // 208341/66317 final frac4 = Fraction.fromString(math.pi, 1.0E-4); // 333/106
picannot be represented as a fraction because it's an irrational number. The constructor considers only
precisondecimal digits to create a fraction. With rational numbers instead you don't have problems.
Thanks to extension methods you can also create a
Fraction object "on the fly" by calling the
toFraction() method on a number or a string.
final f1 = 5.toFraction(); // 5/1 final f2 = 1.5.toFraction(); // 3/2 final f3 = "6/5".toFraction(); // 6/5
The class has a rich API that overloads a lot of operators, for arithmetics and comparisons. The method
reduces reduces the fraction to the lowest terms.
final fraction = Fraction.fromString("12/20"); // 12/20 fraction.reduce(); // now it's simplified to 3/5
Two fractions are equal if their "cross product" is equal. For example
3/6 are said to be
1*6 = 3*2 (and in fact
3/6 is the same as
1/2). You can get the numerator and
the denominator either with getters...
final fraction = Fraction(3, 10); final num = fraction.numerator; // 3 final den = fraction.denominator; // 10
... or with the
final fraction = Fraction(3, 10); final num = fraction; // 3 final den = fraction; // 10
Working with mixed fractions #
A mixed fraction is made up of a whole part and a proper fraction (a fraction in which numerator <= denominator). It's easy to build a
final f1 = MixedFraction(3, 4, 7); final f2 = MixedFraction.fromDouble(1.5); final f3 = MixedFraction.fromString("1 1/2");
There is also the possibility to initialize a
MixedFraction using extension methods:
final f1 = "1 1/2".toMixedFraction();
If you try to create an instance in which the numerator is greater than the denominator, a
MixedFractionException is thrown.
- Added support for
operator (int index)
- Fixed some health suggestions.
- Added a few examples in the repository.
MixedFractionhave been released.
- added support to extension methods to create helpers for
fraction- examples on how to use the
mixed- examples on how to use the
Use this package as a library
1. Depend on it
Add this to your package's pubspec.yaml file:
dependencies: fraction: ^1.1.0
2. Install it
You can install packages from the command line:
$ flutter pub get
Alternatively, your editor might support
flutter pub get.
Check the docs for your editor to learn more.
3. Import it
Now in your Dart code, you can use:
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
- Dart: 2.8.4
- pana: 0.13.14
- Flutter: 1.17.5
Package not compatible with SDK dart
fractionthat is a package requiring null.