Formatting a fraction as a mixed number
Here is a definition for mixedForm
that works for all cases, i.e. proper and improper fractions and integers.
Clear[mixedForm]
mixedForm[Rational[x_, y_]] :=
If[Abs@x > y, HoldForm[#1 + #2/y], x/y] & @@ (Sign@x QuotientRemainder[Abs@x, y])
mixedForm[x_Integer] := x
Some examples:
mixedForm /@ {2, 4/5, 10/3, -3/4, -5/2}
Out[1]= {2, 4/5, 3 + 1/3, -3/4, -2 - 1/2}
Compare with Eli's, which produces 0
s if the number is an integer or a proper fraction
ImproperForm /@ {2, 4/5, 10/3, -3/4, -5/2}
Out[2]= {2 + 0, 0 + 4/5, 3 + 1/3, -1 + 1/4, -3 + 1/2}
Another solution based on FractionalPart
and IntegerPart
would be :
Fraction[x_Rational]:=
Function[{z, y}, If[z!=0, HoldForm[z + y], HoldForm[y]],
{HoldAll}] @@ {IntegerPart[x], FractionalPart[x]}
Fraction[x_Integer] := x
This approach produces slightly different results than R.M.'s solution :
Fraction /@ {2, 4/5, 10/3, -3/4, -5/2}
{2, 4/5, 3 + 1/3, -3/4, -2 -1/2 }
Using the function from the notebook here:
ImproperForm[x_] :=
Function[{z, y}, HoldForm[z + y], {HoldAll}] @@ {Floor[x],x - Floor[x]}
Usage:
In[12]:= ImproperForm[10/7]
Out[12]= 1+3/7