12

To use package{calc} for calculation, number should not include any commas. So I tried to use \def\commatonone #1,#2.00 {#1#2} to get the number without commas.

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

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

192500

However, if I use command to define the number, error happens.

\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. []

Are there any way to fix it? Or any better way to do the calculation with the number having commas already? (numbers are input from other files, so cannot change from raw data). And I hope it can be used for numbers like xxx.xx, xxx,xxx.xx, xxx,xxx,xxx.xx etc.

2
  • Could you add an example of calculations you're performing? – egreg Dec 14 '15 at 9:26
  • how are the numbers input from files ? perhaps a \catcode`, 9 before the \input (and \catcode44 12 after) could work, depending on the file contents. But you also have the issue of the decimal part .00 which can not be used, even with calc as is in a \addtocounter. (this is why David's answer removes it) – user4686 Dec 14 '15 at 18:47
11

This works for any number of commas and more than one level of macro expansion. It assumes that .00 is always present, making it optional would also be possible but complicate the coding a bit.

\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}
7

The following minor modification seems to work:

\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}

(The only change is removing the spaces from your definition of \commantonone and repackaging it in \commatononei, this new command is then called by \commatonone which includes an \expandafter to handle the case where the number is defined by a command.)

3
  • I am not well-versed enough in TeX internals to explain to you exactly what went wrong with your original try. This 'solution' is arrived at from the "guess-and-try" department. – Willie Wong Dec 14 '15 at 6:31
  • Thanks, do you have any idea how to make it work for any numbers with possible two or more commas like xxx.xx, xxx,xxx.xx, xxx,xxx,xxx.xx etc. – Joe Dec 14 '15 at 7:59
  • @Joe: David's answer is what I would try (by recursion), if limited to the world of TeX. – Willie Wong Dec 14 '15 at 14:34

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.