# 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. [...]
isFractionGlyph bool
Returns `true` if this Fraction instance has an unicode glyph associated. For example: [...]
isImproper bool
Returns `true` if the numerator is equal or greater than the denominator. [...]
isNegative bool
True or false whether the fraction is positive or negative.
isProper bool
Returns `true` if the numerator is smaller than the denominator. [...]
isWhole bool
True of false whether the fraction is whole (which is when the denominator is 1).
numerator int
The numerator of the fraction.
late, final
runtimeType Type
A representation of the runtime type of the object.

## Methods

compareTo(Fraction other) int
Compares this object to another object. [...]
override
copyWith({int? numerator, int? denominator})
Creates a deep copy of this object with the given fields replaced with the new values.
inverse()
The numerator and the denominator of the current object are swapped and returned in a new Fraction instance.
negate()
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()
Reduces the current object to the lowest terms and returns the result in a new Fraction instance.
toDouble()
A floating point representation of the fraction.
toEgyptianFraction() List<Fraction>
Represents the current fraction as an egyptian fraction. [...]
toMixedFraction()
Converts the current object into a MixedFraction.
toString()
A string representation of this object. [...]
override
toStringAsGlyph()
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)
Multiplication between two fractions.
operator +(Fraction other)
Sum between two fractions.
operator -(Fraction other)
Difference between two fractions.
operator /(Fraction other)
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.