删除数字内的逗号

删除数字内的逗号

用于package{calc}计算时,数字不能包含逗号。所以我尝试使用\def\commatonone #1,#2.00 {#1#2}不带逗号的数字。

\documentclass{article}
\usepackage{calc}
\def\commatonone #1,#2.00 {#1#2}

\begin{document}
\commatonone 192,150.00
\end{document}

192500

但是,如果我使用命令来定义数字,就会发生错误。

\documentclass{article}
\usepackage{calc}
\def\commatonone #1,#2.00 {#1#2}
\newcommand{\BASESALARYAMOUNT}{149,500.00}
\newcommand{\PREVIOUSBASESALARYAMOUNT}{110,000.00}

\begin{document}

\newcounter{BASESALARYDIFFERENCE}
\addtocounter{BASESALARYDIFFERENCE}{\commatonone\BASESALARYAMOUNT}
\addtocounter{BASESALARYDIFFERENCE}{\commatonone\PREVIOUSBASESALARYAMOUNT}

\end{document}
Error: Paragraph ended before \commatonone was complete. []

有什么方法可以解决这个问题吗?或者有没有更好的方法对已经带有逗号的数字进行计算?(数字是从其他文件输入的,因此无法从原始数据更改)。我希望它可以用于诸如xxx.xx、等数字xxx,xxx.xxxxx,xxx,xxx.xx

答案1

这适用于任意数量的逗号和多于一层的宏扩展。它假设.00始终存在,使其成为可选的也是可能的,但会使编码稍微复杂一些。

\documentclass{article}
\usepackage{calc}

\def\commatonone{\expandafter\zappointzerozero
                 \romannumeral`\^^@}

\def\zappointzerozero#1.00{\zapcomma#1,!}
\def\zapcomma#1,#2{#1\ifx!#2\else#2\expandafter\zapcomma\fi}


\newcommand{\BASESALARYAMOUNT}{149,500.00}
\newcommand{\PREVIOUSBASESALARYAMOUNT}{110,000.00}
\newcommand{\BIGSALARYAMOUNT}{110,110,000.00}

\newcommand\extramacro{\BASESALARYAMOUNT}

\begin{document}

\newcounter{BASESALARYDIFFERENCE}
\addtocounter{BASESALARYDIFFERENCE}{\commatonone\BASESALARYAMOUNT}
\addtocounter{BASESALARYDIFFERENCE}{\commatonone\BIGSALARYAMOUNT}
\addtocounter{BASESALARYDIFFERENCE}{\commatonone\extramacro}

\end{document}

答案2

以下小修改似乎有效:

\documentclass{article}
\usepackage{calc}
\def\commatononei#1,#2.00{#1#2}
\def\commatonone#1{\expandafter\commatononei#1}
\newcommand{\BASESALARYAMOUNT}{149,500.00}
\newcommand{\PREVIOUSBASESALARYAMOUNT}{110,000.00}

\begin{document}

\commatonone\BASESALARYAMOUNT

\newcounter{BASESALARYDIFFERENCE}
\addtocounter{BASESALARYDIFFERENCE}{\commatonone\BASESALARYAMOUNT}
\addtocounter{BASESALARYDIFFERENCE}{\commatonone\PREVIOUSBASESALARYAMOUNT}


\end{document}

(唯一的变化是从您的定义中删除空格\commantonone并将其重新打包,然后通过其中包括\commatononei来调用这个新命令来处理数字由命令定义的情况。)\commatonone\expandafter

相关内容