I want to read the user input and store it as a Rational, whatever the type: integer, float ot rational. For instance:
5 --> store it as 5//1
2.3 --> store it as 23//10
4//7 --> store it as 4//7
At the moment I wrote the following:
a = convert(Rational,parse(Float64,readline(STDIN)))
which is fine if I input an integer, like 5.
But if I input 2.3, a
stores 2589569785738035//1125899906842624
.
And if I input a fraction (whether in the form 4/7
or the form 4//7
) I get an ArgumentError: invalid number format for Float64
.
How to solve the Float&Rational problems?