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 object is thrown.

Inheritance

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 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 divisor b of the a/b division, which also is the denominator of the associated fraction.
no setteroverride
hashCode int
The hash code for this object.
no setteroverride
isFractionGlyph bool
Returns true if this Fraction object has an associated unicode glyph. 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 this rational number is positive or negative.
no setteroverride
isProper bool
Returns true if the numerator is smaller than the denominator.
no setter
isWhole bool
True or false whether this rational number is whole or not.
no setteroverride
numerator int
The dividend a of the a/b division, which also is the numerator of the associated fraction.
no setteroverride
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

compareTo(Rational other) int
Compares this object to another object.
inherited
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 this object are swapped and returned in a new Fraction object.
negate() Fraction
The sign is changed and the result is returned in new instance.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
reduce() Fraction
Reduces this rational number to the lowest terms and returns the result in a new instance.
override
toDouble() double
A floating point representation of this rational number.
override
toEgyptianFraction() List<Fraction>
Represents this rational number as an egyptian fraction.
override
toMixedFraction() MixedFraction
Converts this 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
The product of two fractions.
operator +(Fraction other) Fraction
The sum between two fractions.
operator -(Fraction other) Fraction
The difference between two fractions.
operator /(Fraction other) Fraction
The division of two fractions.
operator <(Rational other) bool
Checks whether this rational number is smaller than the other.
inherited
operator <=(Rational other) bool
Checks whether this rational number is smaller or equal than the other.
inherited
operator ==(Object other) bool
The equality operator.
override
operator >(Rational other) bool
Checks whether this rational number is greater than the other.
inherited
operator >=(Rational other) bool
Checks whether this rational number is greater or equal than the other.
inherited
operator [](int index) int
Allows retrieving numerator or denominator by index. In particular, ´0´ refers to the numerator and ´1´ to the denominator.