Re: Displaying Mixed Numbers
- To: mathgroup at smc.vnet.net
- Subject: [mg25252] Re: Displaying Mixed Numbers
- From: Jim Graber <jgraber at my-deja.com>
- Date: Sun, 17 Sep 2000 04:47:31 -0400 (EDT)
- References: <8pfdta$cuu@smc.vnet.net> <8phca8$g7m@smc.vnet.net> <Uzyv5.58215$Zh6.61684@ralph.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Thanks for all suggestions posted and emailed. I appreciate the help.
Jim Graber
In article <Uzyv5.58215$Zh6.61684 at ralph.vnet.net>,
"Allan Hayes" <hay at haystack.demon.co.uk> wrote:
> 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.
> >
>
>
Sent via Deja.com http://www.deja.com/
Before you buy.