计算器包的四舍五入数字有问题吗?

计算器包的四舍五入数字有问题吗?

我正在使用计算器软件包对数字进行四舍五入,但在某些情况下遇到了问题。我的做法有问题吗?

我已包含 MWE,取消注释舍入 0.0 的行将阻止文档编译。也许这只是我的机器或设置的问题?

\documentclass[a4paper]{memoir}

\usepackage{calculator}

\begin{document}

    % This is OK
    \ROUND[2]{1.2}{\A}
    The value of A is: \A

    % This is also OK
    \ROUND[2]{0}{\B}
    The value of B is: \B

    % This line kills it
    %\ROUND[2]{0.0}{\C}
    %The value of C is: \C

\end{document}

其输出符合预期:

注释了 killer 行的输出 有任何想法吗?

答案1

该错误出现在内部宏中\@@TRUNCATE,当它与输入值相同时,它应该使用解析后的整数部分:

\documentclass[a4paper]{memoir}

\usepackage{calculator}
\makeatletter
\def\@@TRUNCATE[#1]#2#3{%
    \begingroup
    \INTEGERPART{#2}{\cctr@tempa}%
    \ifdim \cctr@tempa\p@ = #2\p@
       \expandafter\@@@TRUNCATE\[email protected])[#1]{#3}% Change here
    \else
       \expandafter\@@@TRUNCATE#200000.)[#1]{#3}%
    \fi
    \@OUTPUTSOL{#3}}
\makeatother
\begin{document}

    % This is OK
    \ROUND[2]{1.2}{\A}
    The value of A is: \A

    % This is also OK
    \ROUND[2]{0}{\B}
    The value of B is: \B

    % This line kills it
    \ROUND[2]{0.0}{\C}
    The value of C is: \C

\end{document}

(定义中也有一些空格:我已将它们删除。)

另一种方法是使用siunitx

\documentclass[a4paper]{memoir}
\usepackage{siunitx}
\sisetup{round-mode = places, round-integer-to-decimal}
\begin{document}

    % This is OK
    The value of A is: \num{1.2}

    % This is also OK
    The value of B is:  \num{0}

    % This line kills it
    The value of C is:  \num{0.0}

\end{document}

相关内容