![计算器包的四舍五入数字有问题吗?](https://linux22.com/image/381349/%E8%AE%A1%E7%AE%97%E5%99%A8%E5%8C%85%E7%9A%84%E5%9B%9B%E8%88%8D%E4%BA%94%E5%85%A5%E6%95%B0%E5%AD%97%E6%9C%89%E9%97%AE%E9%A2%98%E5%90%97%EF%BC%9F.png)
我正在使用计算器软件包对数字进行四舍五入,但在某些情况下遇到了问题。我的做法有问题吗?
我已包含 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}
其输出符合预期:
答案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}