MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Displaying Mixed Numbers

  • To: mathgroup at smc.vnet.net
  • Subject: [mg25158] Re: Displaying Mixed Numbers
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Tue, 12 Sep 2000 02:58:46 -0400 (EDT)
  • References: <8pfdta$cuu@smc.vnet.net> <8phca8$g7m@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Here is are extensions of Paul's code to deal with negative fractions and
held expressions. The separate treatment for held expressions is needed
because the conversion of, for example,  4/3 to Rational[4,3] is part of the
evaluation.

Unprotect[Rational, Times];
Format[Rational[num_Integer, den_Integer] /; Abs[num] > den, StandardForm]
:=
    With[{s = Sign[num/den], an = Abs[num]},
      DisplayForm[
        RowBox[{ToString[s Quotient[ an, den]],
            FractionBox[ToString[Mod[an, den]], ToString[den]]}]]
      ];

Format[(num_Integer/den_Integer) /; Abs[num] > Abs[den], StandardForm] :=
  With[{s = Sign[num/den], an = Abs[num], ad = Abs[den]},
    DisplayForm[
      RowBox[{ToString[s Quotient[ an, ad]],
          FractionBox[ToString[Mod[an, ad]], ToString[ad]]}]]
    ]
Protect[Rational, Times];

--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

"P.J. Hinton" <paulh at wolfram.com> wrote in message
news:8phca8$g7m at smc.vnet.net...
> In article <8pfdta$cuu at smc.vnet.net>,
>  Jim Graber <jgraber at my-deja.com> writes:
>
> > While working with my third grader, I discovered the need
> > to make 5/3 display and print as 1 2/3 so she can check her
> > homework.
> >
> > I couldn't find a way to do this easily, so I wrote the following which
> > does the job.  Is there an easier or better way?
> > Thanks.  Jim Graber
> >
> > Mix1[x_] := { Quotient[Numerator[x], Denominator[x]],
> >     Mod[Numerator[x], Denominator[x]]/Denominator[x]}
> >
> > Mix[x_] := Print[Mix1[x][[1]], Mix1[x][[2]]];
>
> You may want to try adding a special formatting rule for Rational[]
> when the numerator is greater than the denominator.
>
> Unprotect[Rational];
>
> Format[Rational[num_Integer, den_Integer], StandardForm] :=
>   DisplayForm[
>     RowBox[
>       {
>         ToString[Quotient[num, den]],
>         FractionBox[ToString[Mod[num, den]], ToString[den]]
>       }
>     ]
>   ] /; num > den
>
> Protect[Rational];
>
> --
> P.J. Hinton
> User Interface Programmer                         paulh at wolfram.com
> Wolfram Research, Inc.
> Disclaimer: Opinions expressed herein are those of the author alone.
>




  • Prev by Date: Re: Re: Security considerations in Mathematica&J/Link
  • Next by Date: Re: Re: Simple integral wrong
  • Previous by thread: RE: Displaying Mixed Numbers
  • Next by thread: Re: Displaying Mixed Numbers