Decode periodic decimal fractions [FINALIZED]
Rational numbers in decimal representation can have an infinite periodic part. One common way to write this down is to repeat the periodic digits and then add three dots. Numbers without those three dots are taken to be nonperiodic (or equivalently, have a periodic 0
after the given digits). Your task is to decode this representation into a fully cancelled fraction.
In particular, the numbers to decode are given as follows:

There is an optional sign (+ or ). If omitted, + is assumed.

There is an integral part. If empty, it is taken to be 0.

There is a decimal point, which is optional if the number is an integer and the integral part was not omitted.

There is an fractional part following the integer.

If the fractional part has at least one digit, it may be followed by three dots.
The program shall take a string as input, and give a fully cancelled fraction (as pair numerator/denominator) as result. It may assume that the given string conforms to the number format.
The string has to be interpreted as follows:

If the string does not end in three dots, it is interpreted as exact number.

If the string does end in three dots, find the longest repeating digit sequence in the fractional part preceding the three dots. If no such repeating sequence is found, the period consists of just the last digit. Otherwise it consists of that longest repeating digit sequence.

Output the fraction that corresponds to the determined periodic digit sequence. The denominator shall be positive. The fraction shall be completely cancelled.

Your code must handle at least up to 6 digits before and up to 6 digits after the decimal point.
Test cases:
"0" > 0/1
"0" > 0/1
"+0.0" > 0/1
"42" > 42/1
"+2" > 2/1
"6" > 6/1
"2.0" > 2/1
"0815" > 815/1
"." > 0/1
"+." > 0/1
"." > 0/1
".0" > 0/1
"+00.0" > 0/1
".2" > 1/5
"3.14" > 157/50
".3..." > 1/3
"+.11..." > 1/9
"1.0..." > 1/1
"2.9..." > 3/1
"0.121..." > 109/900
"0.1212..." > 4/33
"0.12121..." > 4/33
"0.12122..." > 1091/9000
".122122..." > 122/999
".022122..." > 1991/90000
".0221221..." > 221/9990
"123456.123434..." > 611107811/4950
1 comment thread