8

I'm working on an invoice document which has a 'Grand Total' at the end of the document after some rows of invoice items. As such, I need to keep a running count of the total for each row. Using the traditional \newcounter and \addtocounter counter doesn't work as this doesn't accept decimals, which currency often has.

What would be the recommended way to create and maintain a variable that can have numbers added to it that contain decimals?

percusse
  • 157,807
user27875
  • 163
  • 11
    work in pennies, and put the decimal points in when printing? – Thruston Aug 14 '14 at 13:14
  • 4
    @Thruston My mom always told me to "count your pennies and the dollars take care of themselves." I never knew she had familiarity with LaTeX. – Steven B. Segletes Aug 14 '14 at 13:22
  • 3
    See this for an answer http://tex.stackexchange.com/a/44216/963, uses the 'fp' package. If is ok with you please close this as duplicate. – yannisl Aug 14 '14 at 13:38
  • I think Thruston's suggestion is the best idea. There is a calc package for calculations with decimal points, but it involves truncation at the end of single operations, which you surely do not want. Still, you may be interested in seeing what it offers. – MickG Aug 14 '14 at 14:10
  • But then I hadn't yet seen the other comment with the link. – MickG Aug 14 '14 at 14:11
  • Decimal calculation is sometimes used in the form of lengths, and then the unit of measure is stripped when printing (using something like \strip@pt from the LaTeX kernel). – Werner Aug 14 '14 at 14:15
  • Can you show a simple example of what you have in mind? – egreg Aug 14 '14 at 17:02
  • Have a look on the calculator package, it eases the usage of floating point calculations, but it is not possible with counters, however, in a direct way –  Aug 14 '14 at 20:01
  • Alternatively have a look on the invoices package. It might do what you request –  Aug 14 '14 at 20:03
  • Yiannis's answer using the fp package worked! Thanks! – user27875 Aug 16 '14 at 01:52

2 Answers2

5

Here's a very 'easy' approach with expl3 syntax and its \fp_... variables.

Since the question is not totally clear, I provided some macros for adding and subtracting as well as resetting and displaying the total sum.

\documentclass{article}

\usepackage{xparse}


\ExplSyntaxOn
\fp_new:N \g_total_sum_fp

\NewDocumentCommand{\AddValue}{m}{
  \fp_gadd:Nn \g_total_sum_fp {#1}
}

\NewDocumentCommand{\SubValue}{m}{
  \fp_gsub:Nn \g_total_sum_fp {#1}
}


\NewDocumentCommand{\ResetSum}{}{
  \fp_zero:N \g_total_sum_fp 
}


\NewDocumentCommand{\DisplaySum}{}{
  \fp_to_decimal:N \g_total_sum_fp
}

\ExplSyntaxOff


\begin{document}


\AddValue{4.5}

\DisplaySum

\AddValue{10.5}
\DisplaySum

\AddValue{2}
\DisplaySum

\AddValue{17}
\DisplaySum

\AddValue{-2}
\DisplaySum

\ResetSum
\DisplaySum

\end{document}

enter image description here

A related question can be found here: Number formatting, addtocounter and calc package

  • 1
    @HarishKumar: Hello Harish, you're back? Happy new year! –  Jan 01 '16 at 07:41
  • Hi Christian: I am always there ;). I am on and off but unfortunately the frequency is very low. : –  Jan 30 '16 at 01:34
  • 1
    @HarishKumar: Too low, in my opinion. We miss you –  Jan 30 '16 at 09:59
2

For counting of money you can use normal TeX counter. This counter stores values in pennies.

\newcount\tmpvar
\newcount\myvar 

\def\addvar#1#2 {\let\tmp=#1\addvarA #2.\end}
\def\addvarA#1.#2\end{\advance\tmp by #100
   \ifx;#2;\else\ifx.#2\else\addvarB#1.#2\fi\fi}
\def\addvarB#1.#2#3.{\advance\tmp by \ifnum#1<0-\fi #2#3\ifx;#3;0\fi\space}
\def\printvar#1{\tmpvar=#1%
   \divide\tmpvar by100
   \ifnum\tmpvar=0 \ifnum#1<0-\fi\fi \the\tmpvar
   \multiply\tmpvar by-100 \advance\tmpvar by#1%
   \ifnum\tmpvar=0 \else.%
      \ifnum\tmpvar<0 \tmpvar=-\tmpvar \fi
      \ifnum\tmpvar<10 0\fi \the\tmpvar
   \fi
}

% test:

\myvar=0
\addvar\myvar  3.45
\addvar\myvar -5
\addvar\myvar  1.5
\printvar\myvar  % -0.05 is printed

\myvar=0
\addvar\myvar  2.3
\printvar\myvar  % 2.3 is printed

\myvar=0
\addvar\myvar 123.
\addvar\myvar   0.01
\printvar\myvar  % 123.01 is printed
wipet
  • 74,238
  • What would be needed to use a macro value as the value to add? For example:
    \def\foo{3.14}
    \myvar=0
    \addvar\myvar\foo
    \printvar\myvar % I should expect this to produce 3.14
    
    – Jacob House Dec 12 '22 at 03:44