Skip to main content

ezFraction

ezFraction is a custom Python module for working with fractions.

Installation

From PyPI:

pip install ezFraction

From source:

git clone https://github.com/GooDeeJaY/ezFraction.git
cd ezFraction
python -m pip install .

Usage

Importing and initialization:

from ezFraction import Fraction

# passing string
Fraction("3.14")        # 157/50

# passing float
Fraction(1.24)          # 31/25

# passing int
Fraction(567)           # 567/1

# passing fraction
Fraction(28, 3)         # 28/3

Properties:

frac = Fraction(4.6)

# Properties
print(frac)             # 23/5
print(frac.decimal)     # 4.6
print(frac.numerator)   # 23
print(frac.denominator) # 5
print(frac.tuple)       # (23, 5)

By default fractions will be rounded to 5 decimal places, if you want more or less precision you can set different rounding or even switch off the rounding:

print(Fraction(3.14))                   # 333333/100000
print(Fraction(3.14, rounding=None))    # 4166666666666667/1250000000000000

When initializing Fraction, we can set reduce to False in order to get unreduced fraction:

print(Fraction(18.5))                   # 37/2
print(Fraction(18.5, reduce=False))     # 185/10

Also, there are .reduce() and .enlarge() methods that perform actions that their name tells. Both methods have new_obj parameter which is used to create new Fraction instances rather than applying methods to themselves. Apart from .reduce(), .enlarge() accepts one additional argument multiplier that is used to set the different multiplier for enlargement rather than 2 (default).

frac = Fraction(20.5) 
print(frac)                         # 41/2
print(frac.enlarge())               # 82/4
print(frac.enlarge(multiplier=3))   # 246/12
frac2 = frac.reduce(new_obj=True)
print(frac2, frac)                  # 41/2 246/12

You can use .with_denominator() method in order to set custom denominator:

frac = Fraction(0.32)                               # 8/25
custom = frac.with_denominator(35, to_str=True)     # 11.2/35

Since this creates an invalid fraction, conversion will not be saved in Fraction instances but rather will be returned as a tuple (default) or string. Also, there is a .with_numerator() method that does the same thing but with a numerator.

These methods can be useful when comparing fractions with others. For example, we have fractions 8/25 and 9/35, telling which one is greater by glance is not so easy. So here comes the aid of .with_denominator() method by which we convert 8/25 to 11.2/35. Now it is easy to tell that 11.2/35 is greater than 9/35

Last but not least, you can perform arithmetic operations with Fraction objects:

frac1 = Fraction(0.48)      # 12/25
frac2 = Fraction(1.52)      # 38/25

print(frac1 + frac2)        # 2/1
print(frac1 - frac2)        # -26/25
print(frac1 * frac2)        # 456/625
print(frac1 / frac2)        # 6/19

Metadata

Release files for ezFraction 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ezFraction 1.0.0
File Size Uploaded Provenance Actions
ezFraction-1.0.0.tar.gz 4.6 kB Without provenance Details for ezFraction-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ezFraction 1.0.0
File Interpreter ABI Platform Actions
ezFraction-1.0.0-py3-none-any.whl Python 3 none any Details for ezFraction-1.0.0-py3-none-any.whl

Total release size: 9.1 kB

Release files / ezFraction-1.0.0.tar.gz

Download URL ezFraction-1.0.0.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
928773014ea531ea5fcdc0a78a08f027591efc69db44cfd3386b1a03e1128458
BLAKE2b-256 checksum
How to use checksums
1c04a089b9aaab7083683fa51b768807e184668f56b5f5e196aedb3983f19ff3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.3

Release files / ezFraction-1.0.0-py3-none-any.whl

Download URL ezFraction-1.0.0-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f77d5d582c9a6b598ad9299ff89c5c6b9464dd39dbbca131cd6a520527a7bd4b
BLAKE2b-256 checksum
How to use checksums
a22a2798b3a9ba83ce02c10fb41231076857cf658b5b1c1d439b976a37e567d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.3

Security for ezFraction 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page