Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

enter image description here

share|improve this question
I didn't even think of Mathematica being covered on the original StackOverflow site. – Warren P Dec 7 '12 at 19:06
up vote 11 down vote accepted

Here's another approach:

repeatingForm[x_Rational] := Module[
   {realDigits, integerPart, fractionalPart, start, repeat, exp},
   integerPart = IntegerPart[x];
   fractionalPart = FractionalPart[x];
   realDigits = RealDigits[fractionalPart];
    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

enter image description here

share|improve this answer
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:


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

as seen from

N[237/14, 30]

enter image description here

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]

enter image description here

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

enter image description here

--- via internet connection ---

You can use built-in Wolfram Alpha integration:

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

Test this out:


enter image description here

share|improve this answer
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.

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]]]}, 
        Row[{Numerator[r], " /", Denominator[r]}]], 
       Selectable -> False, ShowStringCharacters -> False]
   InterpretationBox[boxes, r]


Example1 Example2

Here's examples of truncating long repeating decimals Example3
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.

share|improve this answer
+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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.