This repository contains a Python program that converts strings representing rational numbers, in decimal notation, to simplified fractions in form "n/m" where n and m are integers.
If the number is a repeating decimal, its period should be between round brackets since this is the easier way to represent it using a regular keyboard.
For example: -2,5(37) = -2,537373737373737... = -1256/495.
Input: "-2,5(37)" results in output: "-1256/495".
This is particularly useful when we have a repeating decimal and want to write it as a simplified fraction. For instance, the number 825,27(123) is equal to which simplified fraction (in form n/m where both n and m are integers)? You can use the program to find out.
This only works with rational numbers because rational numbers are all the numbers and the only numbers that can be written as the divison of two integers, that is the definition of rational numbers. Note that recurring decimals (dízimas infinitas periódica) are rational numbers, and that 0,(9)=1.
This repository consists of the following files:
-
convert_decimals_to_fractions.py where I defined the functions needed and explained the process in the comments;
-
main.py which can be used to convert rational numbers (in decimal notation) to fractions by using convert_decimals_to_fractions;
-
.replit in which I "tell" Repl.it what to do if you run this repository on Repl.it. (Although I didn't have to, because the main program is called main.py);
-
README.md, this file.
Note that operation with floats are not exact so, approximations are a possibility, the program doesn't have infinite precision, and numbers that are too big or too small will be approximated or result in an error...
👋😃👍