Calculate prices

I want to calculate prices in a LaTex file. This means I want to add two numbers with exactly 2 decimal places and I want to multiply these values by an integer.

An other thing is the output format. The numbers should be printed like this: 1.234.567,89 (I want to use this in Germany, and there we write numbers like that).

I know there are many packages for calculation in LaTeX, but I assume/hope there is one especially for money values.

-
Welcome to TeX.SX! Usually, we don't put a greeting or a “thank you” in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you. –  Papiro May 19 '13 at 17:41
I'm not sure that the question is clear enough yet. Do you want to show how the prices are calculated, or do you just want LaTeX to do the calculations in the background and print the result in the desired format? Could you maybe include a small example document in your question that shows what you're trying to achieve? –  Jake May 19 '13 at 17:48

PGF

You might want to take a look at the mathematical capabilities of pgf. I prepared a MWE:

\documentclass{standalone}
\usepackage{pgf}
\usepgflibrary{fpu}
\pgfkeys{
/pgf/fpu = true,
/pgf/number format/.cd,
precision=2,
fixed,
fixed zerofill,
use comma,
1000 sep={.}
}
\begin{document}
\pgfmathparse{2*(1234.56+9786.45)}
\pgfmathprintnumber{\pgfmathresult}
\end{document}


The output will look something like this

To process huge numbers you need to load the fpu library. (Thank you, Jake)

Alternative

Your questions is a little bit unclear, but if it is only about typesetting currency, you could abuse the siunitx package and do something like this

\documentclass{standalone}
\usepackage{marvosym}
\usepackage{siunitx}
\sisetup{
group-separator = {.},
round-mode = places,
round-precision = 2,
output-decimal-marker = {,}
}
\DeclareSIUnit\euro{\EUR}
\begin{document}
\SI{1234567.8499999}{\euro}
\end{document}


Which leads to the following output

-

Henri Menke's answer is very good, but does not cover the case where you want to perform operations on large numbers before displaying them (the range of pgfmath is rather limited). Instead of pgfmath I would use l3fp, which manipulates floating point numbers with 16 significant digits, and a wide range of exponents. That should be enough for you, as long as you don't manipulate quantities above 10^{16} cents (above that, there will be rounding errors).

\documentclass{standalone}
\usepackage{marvosym}
\usepackage{siunitx}
\sisetup{
group-separator = {.},
round-mode = places,
round-precision = 2,
output-decimal-marker = {,}
}
\DeclareSIUnit\euro{\EUR}
\usepackage{expl3,xparse}
\ExplSyntaxOn
\NewDocumentCommand {\SIeval} {omom}
{ \SI [#1] { \fp_eval:n {#2} } [#3] {#4} }
\ExplSyntaxOff
\begin{document}
\SIeval{(1234567.85 + 252346.42) * 1.196}{\euro}
\end{document}


The fp package can also be used, with a slightly less practical syntax, but wider range, as it manipulates fixed point numbers with 18 digits before and after the decimal point (if I remember correctly).

-
You can make Henri's solution work for huge numbers as well, just by adding \usepgflibrary{fpu} \pgfkeys{/pgf/fpu=true} in the preamble, without any further adjustments. That way, the comfortable syntax of pgfmath is still available. –  Jake May 20 '13 at 1:51
10^16 cents is \$100 Trillion, which is 6 x US debt. You really should watch out for those rounding errors! –  Nicholas Hamilton May 20 '13 at 1:55
@Jake Correct. I find the expandability of l3fp very practical: no need for a two-step process. Instead of \pgfmathparse{3+4} then \dosomethingwith{\pgfmathresult}, directly do \dosomethingwith{\fpeval{3+4}} (after defining \fpeval as an alias of \fp_eval:n). –  Bruno Le Floch May 20 '13 at 2:46
@ADP: true, and the problem is more pronounced for weaker currencies. One day I'll get time to write an arbitrary-precision package (besides the existing bigintcalc, which manipulates integers). –  Bruno Le Floch May 20 '13 at 2:48
@Bruno Whats a couple of trilion between friend(ly nation)s –  Nicholas Hamilton May 20 '13 at 4:19

I would go here with an almost orthogonal answer/comment. The original TeX engine has only integer arithmetic and while TeX is Turing complete language and implementation of floating point arithmetics via integer arithmetic is certainly possible (I believe the name of the package is float-point or something like that) this is a perfect example where using actually LuaTeX engine or PythonTeX package with any engine except the original one seems to me philosophically right way to do this.

I just used PythonTeX over the weekend for the first time as a part of my day job and it is fantastic.

This is quick and dirty lua version. You must use lualatex for this to work!

\documentclass{standalone}
\usepackage{luacode}
\begin{document}

\begin{luacode*}
result = 2*(1234.56+9786.45)
out = string.format("%.2f",result)
tex.print(out)
\end{luacode*}

\end{document}


It is almost obvious that you can replace entire Lua code with

\documentclass{standalone}
\begin{document}
\directlua{tex.print(string.format("%.2f",2*(1234.56+9786.45)))}
\end{document}


Here is PythonTeX. You must run pdflatex, lualatex, or xelatex once then pythontex script then one more time the engine. It doesn't work with Don's engine!

\documentclass{standalone}
% Begin engine=specific settings
\expandafter\ifx\csname pdfmatch\endcsname\relax
\else
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\fi
% xetex:
\expandafter\ifx\csname XeTeXinterchartoks\endcsname\relax
\else
\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}
\fi
% luatex:
\expandafter\ifx\csname directlua\endcsname\relax
\else
\usepackage{fontspec}
\fi
% End engine-specific settings

% Generic packages for PythonTeX.
\usepackage{amsmath,amssymb}
\usepackage{fullpage}
\usepackage{graphicx}
\usepackage[svgnames]{xcolor}
\usepackage{url}
\urlstyle{same}

\usepackage[makestderr]{pythontex}
\restartpythontexsession{\thesection}
\usepackage[framemethod=TikZ]{mdframed}
\begin{document}

\begin{pylabcode}
result = 2*(1234.56+9786.45)
print("{0:.2f}".format(result))
\end{pylabcode}

\end{document}


As people can see most of the above file are just setting :) Note also that Python has much much better ways to deal with currency computations. Consider above just a toy example.

-
I am not so sure about the "philosophically right way". What is your discriminating property for "right" vs. "wrong"? Just that there is no built-in support for it in plain TeX? Would you also consider beamer or TikZ as "philosophically wrong"? The various floating-point packages are neither big nor black magic, so why exactly should one not use them? –  Daniel May 20 '13 at 18:58
@Daniel Give me few more minutes to refine the lua code and to get you pythontex version. –  Predrag Punosevac May 20 '13 at 19:41
@Daniel Check out the examples. I apologize for not posting them on the first place I am just super busy guy. –  Predrag Punosevac May 20 '13 at 20:47
+1 for the examples, I will remove my respective comment. The python thing seems to require lots of extra packages, though I am still not philosophically convinced :-) –  Daniel May 21 '13 at 5:53
In this case, most or all of the packages besides pythontex could be removed, depending on what is wanted. The packages at the beginning are engine-specific, in case a document needs to work under multiple engines while supporting UTF-8, while the others are document-specific. This would be a good problem for Python's decimal module, or perhaps a currency-specific library. –  G. Poore May 21 '13 at 11:50

Pweave

...and here is the Pweave example. It requires only two compilation steps; one less than PythonTeX.

An introductory article to Pweave (and Sweave) can be found here.

The first comment line explains how to install Pweave on a GNU/Linux system. The second line of comment indicates how to compile the .Pnw file in order to obtain the .tex and .pdf files.

In this example, Python uses the system locales to determine the decimal and thousands grouping symbols, as well as the position and type of the currency symbol.

For this to work, the referenced locales need to be installed on your system. The Python comment explains how to so for a Debian GNU/Linux system.

Finally, the \EUR command of the eurosym package prints the officially correct euro symbol. Again, Python code takes care of the symbol substitution.

%sudo easy_install -U Pweave
%Pweave -f tex filename.Pnw | pdflatex -synctex=1 -interaction=nonstopmode filename.tex
\documentclass{article}

%document encoding
\usepackage[utf8]{inputenc}

%official euro symbol
\usepackage{eurosym}

\begin{document}
<%
from locale import setlocale, LC_ALL, currency

def money(value):
return str(currency(value, grouping=True)).replace('€','\EUR')

amount = 2 * (1234.56 + 9786.45)

#To install additional locales in Debian GNU\Linux: sudo dpkg-reconfigure locales

setlocale(LC_ALL, 'de_DE.utf8')%>
In Deutschland hatt diese L\"{o}sung einen Wert von <%=money(amount)%>.

<%setlocale(LC_ALL, 'de_BE.utf8')%>
In Belgien hatt diese L\"{o}sung einen Wert von <%=money(amount)%>.

<%setlocale(LC_ALL, 'en_IE.utf8')%>
In Ireland, this solution is worth <%=money(amount)%>.

<%setlocale(LC_ALL, 'en_GB.utf8')%>
In the UK, this solution is worth <%=money(0.85504 * amount)%>.
\end{document}

-