[Tex/LaTex] Number formatting, addtocounter and calc package

calculationscountersformatting

I have been having troubles with the following issue:

I am making an invoice template in LaTeX. I use \addtocounter to calculate values like subtotal, tax and total. This works fine, until the value contains a decimal number (which happens often in invoices of course). To counter this, I use the calc package in order to do: \addtocounter{subtotal}{\real{19.95}}. This works fine too.

However, when I load the package numprint or siunitx so that my numbers are correctly formatted on the decimal in the invoice table, I get the error:

/Invoices/A4.tex:22 Missing number, treated as zero \addtocounter{subtotal}{\real{19.95}}

As soon as I remove those two packages, all works fine again.

My question: is there a way to format numbers in a table while still being able to calculate the totals using decimal numbers?

You can find the code here: https://www.overleaf.com/4958478dccytf#/15308055/
Note that the code includes an invoice.cls and that an example of the problem can be found in the main page on line 22 and 23.

Best Answer

The \real macro seems to need arithmetics desperately, i.e. use 1*\real{19.95} etc.

But it will be truncated anyway!

\documentclass{article}

\usepackage{siunitx}
\usepackage{numprint}
\usepackage{calc}


\newcounter{foo}
\begin{document}

\addtocounter{foo}{5*\real{1.7}}

\thefoo

\end{document}

Related: Is there a counter or variable that can be used to store non-integers?