不幸的是,在我们的一些文档中,我们需要打印 SI 单位和英制单位。我们定义了一些宏,如下所示:
\documentclass{article}
\usepackage{fp}
\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)}
\begin{document}
\noindent
\GALLONS{200.00}\\
\GPM{500.00}\\
\LPM{1892.50}
\end{document}
如何修改宏,以便如果您输入一个数字,转换宏会自动获取小数位数,并将转换精度设置为相同的小数位数,即输入\GALLONS{150.000}
应该排版567.750 litres
。
答案1
这是加仑的示例。
\documentclass{article}
\usepackage{fp}
\makeatletter
\newcounter{NoDecim@ls}
\def\getDecim@ls#1{%
\setcounter{NoDecim@ls}{0}%
\getDecim@ls@i#1!!\@nil}
\def\getDecim@ls@i#1#2\@nil{%
\ifx!#1\else\stepcounter{NoDecim@ls}\getDecim@ls@i#2\@nil\fi}
\def\GALLONS#1{\Gallons@i#1..\@nil}
\def\Gallons@i#1.#2.#3\@nil{%
\FPmul\temp{#1}{3.785} #1%
\ifx\relax#2\relax\else%
\FPmul\tempD{#2}{3.785}%
\FPadd\temp\temp\tempD%
.#2%
\fi
\getDecim@ls{#2}%
\FPround\temp\temp{\theNoDecim@ls}
gallons (\temp\ litres)}
\makeatother
\begin{document}
\GALLONS{200.00}\\
\GALLONS{150.000} should typeset 567.750 litres.
\GALLONS{150.00} should typeset 567.75 litres.
\GALLONS{150} should typeset 568 litres.
\end{document}
答案2
\documentclass{article}
\usepackage{xstring}
\usepackage{fp}
\newcount\acc
\begin{document}
\def\gallons#1{\IfDecimal{#1}%
{\StrLen{#1}[\templen]%
\StrPosition{#1}{.}[\temppos]%
\ifnum\temppos=0%
\def\theacc{0}%
\else
\acc=\numexpr\templen-\temppos
\edef\theacc{\the\acc}%
\fi
\FPupn\result{3.785 #1 * \theacc{} round}%
\result
}%
{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
\end{document}