PyDocs

Nav

Numeric and Mathematical Modules

Fraction

  • Fraction(numerator=0, denominator=1)
  • Fraction(number)
  • Fraction(string)
This class implements rational numbers.

In the two-argument form of the constructor, Fraction(8, 6) will
produce a rational number equivalent to 4/3. Both arguments must
be Rational. The numerator defaults to 0 and the denominator
defaults to 1 so that Fraction(3) == 3 and Fraction() == 0.

Fractions can also be constructed from:

  - numeric strings similar to those accepted by the
    float constructor (for example, '-2.3' or '1e10')

  - strings of the form '123/456'

  - float and Decimal instances

  - other Rational instances (including integers)
Changed in 3.2, 3.9, 3.11, 3.12, 3.13, 3.14
  • 3.2: The Fraction constructor now accepts float and decimal.Decimal instances.
  • 3.9: The math.gcd() function is now used to normalize the numerator and denominator. math.gcd() always returns an int type. Previously, the GCD type depended on numerator and denominator.
  • 3.11: Underscores are now permitted when creating a Fraction instance from a string, following PEP 515 rules.
  • 3.11: Fraction implements __int__ now to satisfy typing.SupportsInt instance checks.
  • 3.12: Space is allowed around the slash for string inputs: Fraction('2 / 3').
  • 3.12: Fraction instances now support float-style formatting, with presentation types "e", "E", "f", "F", "g", "G" and "%"".
  • 3.13: Formatting of Fraction instances without a presentation type now supports fill, alignment, sign handling, minimum width and grouping.
  • 3.14: The Fraction constructor now accepts any objects with the as_integer_ratio() method.
Members of Fraction

_

__ceil__
math.ceil(a)
__floor__
math.floor(a)
__format__
Format this fraction according to the given format specification.
__round__
Rounds half toward even.

a

as_integer_ratio
Return a pair of integers, whose ratio is equal to the original Fraction.

d

denominator
Denominator of the Fraction in lowest terms.

f

from_decimal
Converts a finite Decimal instance to a rational number, exactly.
from_float
Converts a finite float to a rational number, exactly.
from_number
Converts a finite real number to a rational number, exactly.

i

is_integer
Return True if the Fraction is an integer.

l

limit_denominator
Closest Fraction to self with denominator at most max_denominator.

n

numerator
Numerator of the Fraction in lowest term.
1 Example
>>> from fractions import Fraction
>>> Fraction(16, -10)
Fraction(-8, 5)
>>> Fraction(123)
Fraction(123, 1)
>>> Fraction()
Fraction(0, 1)
>>> Fraction('3/7')
Fraction(3, 7)
>>> Fraction(' -3/7 ')
Fraction(-3, 7)
>>> Fraction('1.414213 \t\n')
Fraction(1414213, 1000000)
>>> Fraction('-.125')
Fraction(-1, 8)
>>> Fraction('7e-6')
Fraction(7, 1000000)
>>> Fraction(2.25)
Fraction(9, 4)
>>> Fraction(1.1)
Fraction(2476979795053773, 2251799813685248)
>>> from decimal import Decimal
>>> Fraction(Decimal('1.1'))
Fraction(11, 10)
from the Python docs
See Also
No see-alsos for fractions/Fraction
0 Notes
No notes for Fraction