Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decimal to Fraction Implementation #18

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions scicalc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ def log10(x):

return math.log10(x)

def dec_to_frac(x):
"""Return a fraction representation of the decimal"""

# Find the number of decimal places
dp = len(str(x).split(".")[1])
# check how many decimal places
# denominator set as how many decimal places there are
# find the common factor between the two
denominator = 10**int(dp)
numerator = x*denominator
# find the greatest common factor
greatest_factor = math.gcd(int(denominator), int(numerator))
denominator /= greatest_factor
numerator /= greatest_factor
frac = str(int(numerator)) + "/" + str(int(denominator))
return frac

#
# The dictionary that maps the command-line name of the operation,
# to the function that performs it. There can be multiple names
Expand All @@ -58,6 +75,7 @@ operators = {
'sum': add,
'mul': mul,
'log10': log10,
'dec_to_frac': dec_to_frac
}

if __name__ == "__main__":
Expand Down