# Can Mathematica show me a fraction with a repeating decimal notation?

The value of 3/817 when expressed using the N[3/817, 100] shows me data without helping me see the repeating patterns:

After I mess around with the results in Notepad, I can reformat it in notepad so I see the repeating decimals, but what I would really like to see is a line over the repeating group of digits, like this:

-
I didn't even think of Mathematica being covered on the original StackOverflow site. –  Warren P Dec 7 '12 at 19:06

Here's another approach:

repeatingForm[x_Rational] := Module[
{realDigits, integerPart, fractionalPart, start, repeat, exp},
integerPart = IntegerPart[x];
fractionalPart = FractionalPart[x];
realDigits = RealDigits[fractionalPart];
Which[
MatchQ[realDigits, {{__Integer, {__Integer}}, _Integer}],
start = Most[First[realDigits]];
repeat = Last[First[realDigits]],
MatchQ[realDigits, {{{__Integer}}, _Integer}],
start = {};
repeat = Last[First[realDigits]],
MatchQ[realDigits, {{__Integer}, _Integer}],
start = First[realDigits];
repeat = {}
];
exp = Last[realDigits];
If[exp < 0,
start = Join[Table[0, {-exp}], start]];
If[Length[repeat] > 0,
Row[Flatten[{N[integerPart], start, OverBar[Row[repeat]]}]],
Row[Flatten[{N[integerPart], start}]]
]
];


Let's try it on Vitaliy's original set of data points. (I guess he deleted this portion.)

data = {98464/3, 2209604/53, 1654407/32, 4695213/44, 2608035/32,
1060220/31, 1254299/54, 3180989/37, 1120269/83, 6084320/73};
repeatingForm /@ data // Column


-
I can tell you why: Because RealDigits needs to be tested. In something like RealDigits[1/2] one inner List missing and when I see this right, this case is not tested. +1 btw. –  halirutan Dec 6 '12 at 7:00
Thanks for noting that, Mark, I fixed the bug. –  Vitaliy Kaurov Dec 6 '12 at 7:19
@VitaliyKaurov Cool! This was definitely a little trickier than I thought it would be when I jumped in! –  Mark McClure Dec 6 '12 at 7:23
Yes, indeed, ;) +1 btw –  Vitaliy Kaurov Dec 6 '12 at 7:33
Thanks. This is really good info for someone just learning how to write functions in mathematica. –  Warren P Dec 6 '12 at 15:43

--- final function ---

I recommend reading article Repeating Decimal. Function RealDigits gives you complete information:

RealDigits[237/14]


{{1, 6, 9, {2, 8, 5, 7, 1, 4}}, 2}

as seen from

N[237/14, 30]


Write a function that shows an overbar and period:

RepeatingDecimal[x_Rational] := With[{tv = RealDigits[x]},
Subscript[Row[Insert[Cases[tv[[1]], _Integer]~Join~
(OverBar /@ Flatten[Cases[tv[[1]], _List]]), ".", tv[[-1]] + 1]]
, Length[tv[[1, -1]]]]]


Test it out:

dat = DeleteCases[(RandomInteger[{1, 10^7}, 5]/RandomInteger[{10, 50}, 5]), _Integer]


Grid[{RepeatingDecimal[#], N[#, 20]} & /@ dat, Alignment -> Left]


--- via internet connection ---

You can use built-in Wolfram Alpha integration:

RepDec[x_Rational] :=
WolframAlpha[ToString[InputForm[x]], {{"RepeatingDecimal", 1}, "Content"}]


Test this out:

RepDec[4692085/38]


-
Your function does not work correctly for 1/2 and e.g. 1060220/31. –  halirutan Dec 6 '12 at 7:02
@halirutan I fixed the bug, thanks. Wolfram Alpha function will work only with "good" numbers for obvious reasons - other numbers do not have corresponding pod. –  Vitaliy Kaurov Dec 6 '12 at 7:13
Wow! This is impressive. –  Warren P Dec 6 '12 at 15:42
@VitaliyKaurov Finally a very nice answer +1. –  halirutan Dec 6 '12 at 23:28
The output looks wonky for my original fraction, which is a very long series of repeating digits. –  Warren P Dec 7 '12 at 3:18

This code will automatically write rationals as repeating (or finite) decimals. The decimals have a tooltip that show which rational they represent and are automatically truncated for long repeating sections. The decimal is not editable, so that it can't be broken when copying (this can probably be improved upon). To edit the number, select it and convert to inputform.

Unprotect[Rational];
Rational /: MakeBoxes[r : Rational[_Integer, _Integer], _] :=
With[{trunc = 45, d = RealDigits[FractionalPart[r]]},
With[{boxes = ToBoxes[Style[Tooltip[
Row[Flatten@{IntegerPart[r], ".", ConstantArray[0, -d[[2]]],
If[Head[d[[1, -1]]] === List, {Most@First@d, OverBar[
Row[If[Length[#] > trunc, Join[#[[1 ;; trunc]],
{"\[LeftSkeleton]", Length@# - trunc, "\[RightSkeleton]"}], #]]&@d[[1, -1]]]},
First@d]}],
Row[{Numerator[r], " /", Denominator[r]}]],
Selectable -> False, ShowStringCharacters -> False]
]},
InterpretationBox[boxes, r]
]]
Protect[Rational];


e.g.,

Here's examples of truncating long repeating decimals
Note that for complicated rational numbers, this code to format them as repeating decimals can take some time, simply due to calculating and displaying the RealDigits.

-
+1 for making the formatted output usable as input. –  Mechanical snail Dec 7 '12 at 0:47
This gets to be a better approach the longer the output sequences get. –  Warren P Dec 7 '12 at 3:20
Is there another command I could use to toggle Rational back to its default behaviour if I found I wanted to toggle it? –  Warren P Dec 7 '12 at 3:32
@WarrenP: It's not a toggle, but you can restore the original behaviour: Unprotect[Rational];Clear[Rational];Protect[Rational]; –  Simon Dec 7 '12 at 12:27
I've also increased trunc to 45, so that it works on your original set of examples. –  Simon Dec 7 '12 at 12:36