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.

  • 2
    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

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.

   \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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.