# Enter exact rational numbers easily with decimal notation

Is there a simple way to enter decimal numbers such as 0.123 so that Mathematica interprets it as an exact rational number 123/1000?

• Rationalize[0.123] – Yi Wang Feb 11 '14 at 19:25
• Rationalize does have limitations, though. – Michael E2 Feb 11 '14 at 19:51
• @MichaelE2 Could you explain it a bit? – Yi Wang Feb 11 '14 at 21:34
• @YiWang Rationalize[0.333333], Rationalize[0.3333333333333], and the explanation in the docs: "Rationalize[x] yields x unchanged if there is no rational number close enough to x to satisfy the condition |p/q-x| < c/q^2, with c chosen to be 10^-4." – Michael E2 Feb 11 '14 at 21:51
• @YiWang You're welcome! :) Even Rationalize[0.333333333, 0] does not do what the OP wants. – Michael E2 Feb 11 '14 at 21:58

No doubt this needs work to make robust:

 fract[s_String] := (ToExpression[StringReplace[s, "." -> ""]]/
10^(StringLength[s] - First@First@StringPosition[s, "."]));

$PreRead = If[Head[#] =!= Real, ( # /. s_String :> StringReplace[s, a : ("." ~~ DigitCharacter ...) ~~ "*^" :> a <> " 10^"] /. s_String :> StringReplace[s, a : ( (DigitCharacter .. ~~ "." ~~ DigitCharacter ...) | (DigitCharacter ... ~~ "." ~~ DigitCharacter ..) ) :> "fract[\"" <> a <> "\"]"] ), #, #] & (1.23 + .1*^4 x) /Sin[ .5 a ]  (123/100 + 1000 x) Csc[a/2] Two known issues: this breaks if you use explicit precision backtic notation, or if your input contains floats within strings. • How about $Pre = # /. r_Real :> RuleCondition[Round[r, 10^-Ceiling@Precision@r]] & – Simon Woods Apr 15 '14 at 16:06
• The point is to work with the actual text entered to avoid some of the issues raised in the comments arising from conversion to/from machine precision. (As a practical matter those issues may not be important, and using \$Pre like that is clearly cleaner and more robust ) – george2079 Apr 15 '14 at 16:57
• May be a good idea to append /; StringMatchQ[s, RegularExpression["[+-]?[0-9]*\\.[0-9]+"]] to your fract: this will avoid all the possible breakage with unexpected formats of the string, returning unevaluated expression instead. – Ruslan Apr 9 '17 at 9:06
• @Ruslan: By "append to the end of fract" do you mean exactly this?: fract[s_String] := (ToExpression[StringReplace[s, "." -> ""]]/ 10^(StringLength[s] - First@First@StringPosition[s, "."]));/; StringMatchQ[s, RegularExpression["[+-]?[0-9]*\\.[0-9]+"]] – theorist Feb 26 '18 at 7:43
• @theorist not quite: insert it before the trailing semicolon — to make the pattern conditional – Ruslan Feb 26 '18 at 7:57

Given the limitations described in the comments, a possibility is to use Ratiolize[x,0] in combination with SetPrecision or SetAccuracy. E.g.:

Rationalize[SetPrecision[0.33333, 5], 0]


1/3

While

Rationalize[SetAccuracy[0.3333, 5], 0] (* one less 3 *)


3332 / 9997

The point is to set Accuracy or Precision to the number of digits that would be normally entered to represent (conventionally) the desired rational as a floating point number.

• I think the OP wants the outputs to be 33333/100000 and 3333/10000 respectively, but I think the simplest way to enter these numbers is 33333*^-5 and 3333*^-4, or as fractions. – Michael E2 Feb 11 '14 at 23:31
• @MichaelE2 it's not really simplest if you want to be able to append digits (e.g. in a manual binary search): you have to carefully count the digits to make sure your order of magnitude isn't off and that you didn't to forget to change the exponent after you appended a new digit. – Ruslan Apr 9 '17 at 8:41
• @Ruslan If counting digits is a factor in a particular case, what's simpler than the second option, fractions, in which counting seems unnecessary? – Michael E2 Apr 9 '17 at 11:55
• @MichaelE2 well, that's better, but inconvenient — it's always best to have to change one thing to get what you need. The simplest way would be to define a function, which would do it all for you, as fract in george2079's answer. – Ruslan Apr 9 '17 at 13:51
• @Ruslan I see what you mean. What seems simpler depends on context. E.g., how many conversions/entries do you have to do and how often. My personal, and admittedly parochial, experience is limited to infrequent and few conversions. Mostly nowadays they arise from converting parameters in SE questions to exact values, although Rationalize is usually sufficient. It hardly seems simpler to dredge up or write a function, when I can type the fraction in more quickly than I can find and type the function command. But it's different for even a moderate amount of data. – Michael E2 Apr 9 '17 at 14:22