The package datatool
gives me some headaches with the currency-type fields.
My data is a test.csv
file exported from Excel, without header:
a,"$100,090.06 ","$107,578.03 ",0.129565278
b,"$267,697.13 ","$72,463.54 ",0.087442318
c,"$761,314.81 ","$192,210.83 ",0.231820008
some text
some text2
I'd like to (1) add totals for some columns; and (2) round the figures:
Label Amount1 Amount2 Percent
----------------------------------------
a $100,090 $107,578 13%
b $267,697 $72,464 9%
c $761,315 $192,211 23%
----------------------------------------
Total: $1,129,102 45%
Here is my code:
\documentclass[a4paper, 11pt]{article}
\usepackage{datatool}
\begin{document}
\DTLloadrawdb[noheader,headers={label,Amount1,Amount2,\%},keys={A,B,C,D}]{myDB}{test.csv}
Displaying the table:\\
\DTLdisplaydb{myDB} \\
Operating the table:\\
\def\totalAmount{0}
\def\totalPercent{0} %initializing totalAmount and totalPercent to 0
\begin{tabular}{r|ll|r}
label & Amount1 & Amount2 & Percent
\DTLforeach[\value{DTLrowi}<3]{myDB}{\aa=A,\bb=B,\cc=C,\dd=D}{
\\
\aa & \bb & \cc &
\DTLmul{\dd}{\dd}{100}\DTLround{\dd}{\dd}{0}\dd\% %formatting the percent column
\DTLgadd{\totalPercent}{\dd}{\totalPercent} %cumulating percentages
\DTLgadd{\totalAmount}{\bb}{\totalAmount} %cumulating Amount <- THIS LINE DOES NOT WORK (when removing it, the code does partially what I want), probably because \bb is not seen as real
} \\ \hline
Total & \totalAmount & & \totalPercent\%
\end{tabular}
\end{document}
I guess my failure comes the fact that \bb
and \cc
are currencies and not real.
test.csv
contains comma-delimited fields and some random text at the bottom? Is there some way of identifying when the text begins/data ends?