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

Unfortunately, in some of our documents we need to print both SI units as well as Imperial units. We have some macros defined as follows:


\def\GPM#1{#1 gpm\FPmul\temp{#1}{3.785}
\FPround\temp{\temp}{2}(\temp\ lpm) }

\def\GALLONS#1{#1 gallons \FPmul\temp{#1}{3.785}
\FPround\temp{\temp}{2}(\temp\ litres)}

\def\LPM#1{#1 lpm\FPdiv\temp{#1}{3.785}
\FPround\temp{\temp}{2}(\temp\ gpm)}


enter image description here

How can one modify the macros, so that if you type a number, the conversion macro picks up the number of decimals automatically and sets the conversion accuracy to the same number of decimals, i.e., typing \GALLONS{150.000} should typeset 567.750 litres.

share|improve this question
Why are you not using siunitx for the units. I can also be used for the rounding. The number of fractional digits can be counted by an recursive macro which reads each a digit a time and increments an counter. – Martin Scharrer Feb 11 '11 at 18:47
@Martin I actually took them out to simplify the example! – Yiannis Lazarides Feb 11 '11 at 18:55
You can try xstring package to detect the accuracy. – Leo Liu Feb 11 '11 at 19:05
up vote 5 down vote accepted

Here is an example for gallons.

enter image description here



  \FPmul\temp{#1}{3.785} #1%
  gallons (\temp\ litres)}



\GALLONS{150.000} should typeset 567.750 litres.

\GALLONS{150.00} should typeset 567.75 litres.

\GALLONS{150} should typeset 568 litres.
share|improve this answer

   \FPupn\result{3.785 #1 * \theacc{} round}%
  {NaN} litres}

\gallons{?}      % NaN litres

\gallons{150}    % 568 litres

\gallons{150.0}  % 567.8 litres

\gallons{150.00} % 567.75 litres

\gallons{150.000}% 567.750 litres

share|improve this answer

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.