Fraction class

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

Fraction(-2, 5);
Fraction.fromDouble(1.5);
Fraction.fromString("4/5");

There also are extension methods to quickly build Fraction objects from primitive types:

1.5.toFraction();
"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 only appears 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.
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.
final
hashCode int
The hash code for this object.
no setteroverride
isFractionGlyph bool
Returns true if this Fraction instance has an unicode glyph associated. For example:
no setter
isImproper bool
Returns true if the numerator is equal or greater than the denominator.
no setter
isNegative bool
True or false whether the fraction is positive or negative.
no setter
isProper bool
Returns true if the numerator is smaller than the denominator.
no setter
isWhole bool
True of false whether the fraction is whole.
no setter
numerator int
The numerator of the fraction.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

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 nonexistent 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.

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.