Fraction class Null safety

Dart representation of a fraction having both the numerator and the denominator as integers.

You can create a new instance of Fraction either by using one of the constructors or by using the extension methods on num and String.

final f = Fraction.fromDouble(1.5);
final f = Fraction.fromString("4/5");

is equivalent to

final f = 1.5.toFraction();
final f = "4/5".toFraction();

If the string doesn't represent a valid fraction, a FractionException is thrown.

Implemented types

Constructors

Fraction(int numerator, [int denominator = 1])
Creates a new representation of a fraction. If the denominator is negative, the fraction is normalized so that the minus sign can only appear in front of the denominator. [...]
factory
Fraction.fromDouble(double value, {double precision = 1.0E-12})
Tries to give a fractional representation of a double according with the given precision. This implementation takes inspiration from the (continued fraction)https://en.wikipedia.org/wiki/Continued_fraction algorithm. [...]
factory
Fraction.fromGlyph(String value)
Returns an instance of Fraction if the source string is a glyph representing a fraction. A 'glyph' (or 'number form') is an unicode character that represents a fraction. For example, the glyph for "1/7" is ⅐. Only a very small subset of fractions have a glyph representation. [...]
factory
Fraction.fromMixedFraction(MixedFraction mixed)
Converts a MixedFraction into a Fraction.
factory
Fraction.fromString(String value)
Returns an instance of Fraction if the source string is a valid representation of a fraction. Some valid examples are: [...]
factory

Properties

denominator int
The denominator of the fraction.
late, final
hashCode int
The hash code for this object. [...]
read-only, override
isFractionGlyph bool
Returns true if this Fraction instance has an unicode glyph associated. For example: [...]
read-only
isImproper bool
Returns true if the numerator is equal or greater than the denominator. [...]
read-only
isNegative bool
True or false whether the fraction is positive or negative.
read-only
isProper bool
Returns true if the numerator is smaller than the denominator. [...]
read-only
isWhole bool
True of false whether the fraction is whole (which is when the denominator is 1).
read-only
numerator int
The numerator of the fraction.
late, final
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

compareTo(Fraction other) int
Compares this object to another object. [...]
override
copyWith({int? numerator, int? denominator}) Fraction
Creates a deep copy of this object with the given fields replaced with the new values.
inverse() Fraction
The numerator and the denominator of the current object are swapped and returned in a new Fraction instance.
negate() Fraction
The sign of the current object is changed and the result is returned in a new Fraction instance.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
reduce() Fraction
Reduces the current object to the lowest terms and returns the result in a new Fraction instance.
toDouble() double
A floating point representation of the fraction.
toEgyptianFraction() List<Fraction>
Represents the current fraction as an egyptian fraction. [...]
toMixedFraction() MixedFraction
Converts the current object into a MixedFraction.
toString() String
A string representation of this object. [...]
override
toStringAsGlyph() String
If possible, this method converts this Fraction instance into an unicode glyph string. A 'glyph' (or 'number form') is an unicode character that represents a fraction. For example, the glyph for "1/7" is ⅐. Only a very small subset of fractions have a glyph representation. [...]

Operators

operator *(Fraction other) Fraction
Multiplication between two fractions.
operator +(Fraction other) Fraction
Sum between two fractions.
operator -(Fraction other) Fraction
Difference between two fractions.
operator /(Fraction other) Fraction
Division between two fractions.
operator <(Fraction other) bool
Checks whether this fraction is smaller than the other.
operator <=(Fraction other) bool
Checks whether this fraction is smaller or equal than the other.
operator ==(Object other) bool
The equality operator. [...]
override
operator >(Fraction other) bool
Checks whether this fraction is greater than the other.
operator >=(Fraction other) bool
Checks whether this fraction is greater or equal than the other.
operator [](int index) int
Access numerator or denominator via index. In particular, ´0´ refers to the numerator while ´1´ to the denominator.