TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

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

I'm creating an invoice in LaTeX and I've entered the hours I've worked as decimals (for example, 1, 1.5, 0.75, 2.25), but I want to display those decimals as fractions (1, 1½, ¾, 2¼).

(The reason I've entered the hours worked as a decimal is so I can perform simple invoice math with the fp package along the lines of this answer)

For a little context, here's the synopsis of the macro I created that includes the number of hours worked:


The conversions I have in mind are pretty simple:

tex rendered

\begin{tabular}{r @{} l | r @{\hspace{0.1em}} l}
\multicolumn{2}{c}{Given...} & \multicolumn{2}{c}{Output} \\
1 & & 1 & \\
1 & .5 & 1 & \sfrac{1}{2} \\
0 & .75 & \sfrac{3}{4} & \\
2 & .25 & 2 & \sfrac{1}{4} \\

(I'm not married to \sfrac for displaying fractions, it's just the first package I reached for)

Is there an off-the-shelf package that will do what I want? If not, can anyone give me a few pointers how I might go about implementing my own conversion/display logic?

share|improve this question
@PeterGrill That helps, but my problem is not so much reducing the fraction, as it is splitting a decimal into a fraction. I know 2.25 = 2 + 25/100, I get that — I just don't know how to express that in TeX. Yes, my TeX knowledge is very bad. – Michael Kropat May 12 '14 at 19:38
up vote 4 down vote accepted

Adapting the solution from Automatically add fractions and reduce the result (if neccessary) yields:

enter image description here


  • The \hphantom line should be commented out in your actual use case. It was added to produce a nicer output for posting on TeX.SE.
  • This may not yield ideal results for all possible decimals but does work for the ones requested.
  • For the HH:MM format, I used the xstring package for string comparison as I prefer its format, but probably can be done without this package if desired.



    \StrPosition{#1}{#2}[\@PositionOfColon]% Record position in \@PositionOfColon
    {#4}% StrPosition=0 => Did not find the target string
    {#3}% StrPosition>0 => Found the target string

% Use Euclid's Algorithm to find the greatest 
% common divisor of two integers.
\def\gcd#1#2{{% #1 = a, #2 = b
    \ifnum#2=0 \edef\next{#1}\else
        \@tempcnta=#1 \@tempcntb=#2 \divide\@tempcnta by\@tempcntb
        \multiply\@tempcnta by\@tempcntb  % q*b
        \advance\@tempcntb by-\@tempcnta % remainder in \@tempcntb
            \ifnum\@tempcnta < 0 \@tempcnta=-\@tempcnta\fi

%% Adapted from http://tex.stackexchange.com/questions/28628/automatically-add-fractions-and-reduce-the-result-if-neccessary
   \gcd{#1}{#2}{\@tempcnta=#1 \divide\@tempcnta by\thegcd
   \@tempcntb=#2 \divide\@tempcntb by\thegcd


    #1 =
    \ifnum\WholePortion > 0\relax%
        \hphantom{0}% Just for nicer output of MWE. Should be deleted in actual use

Using decimals:



Using \texttt{HH:MM} format:




share|improve this answer
That helps a lot. Now I just need to figure out how to turn it into a mixed fraction. – Michael Kropat May 12 '14 at 20:17
@MichaelKropat: Update to handle whole numbers. – Peter Grill May 12 '14 at 20:29
That works beautifully. The code is about what I expected, except that it would have taken me forever to figure out how to express that on my own, so thanks a bunch. That was the last piece of my invoice, so now I can finally get paid, hopefully :) – Michael Kropat May 12 '14 at 20:41

tikz provides this type of formatting:

enter image description here

  \pgfkeys{/pgf/number format/frac}% Format as fraction
  \let\frac\sfrac% Let \frac act like \sfrac

\begin{tabular}{r @{} l | r @{\hspace{0.1em}} l}
  \multicolumn{2}{c}{Given...} & \multicolumn{2}{c}{Output} \\
  1&    & 1& \\
  1&.5  & 1&\dectofrac{0.5} \\
  0&.75 &  &\dectofrac{0.75} \\
  2&.25 & 2&\dectofrac{0.25}
share|improve this answer
You can also use \dectofrac{2.25} which will be formatted to 2 1/4, but it depends on how you want the alignment to be. – Werner May 12 '14 at 20:49
Very nice. I kinda expected something like this to be included in some package somewhere. – Michael Kropat May 12 '14 at 20:51

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.