[Tex/LaTex] Adjust decimals on table from csv

csvimporttables

My problem is that I'm importing some data from a csv and generating a table.

The table is good, but I need the numbers with 4 decimals. I've generated the table with this code (probably it is not the cleanest one, but it works):

\documentclass{article}

\usepackage{booktabs}
\usepackage{csvsimple}

\newcommand{\dataa}{\begin{tabular}{c} \\  \\ \\ Var1 \\ \\  \\ \\Var2 \\  \end{tabular}}
\newcommand{\datab}{\begin{tabular}{c}  4\\ 8\\12\\16\\4\\ 8\\12\\16\\  \end{tabular}}
\newcommand{\datac}{\csvreader[separator=comma, no head, tabular={c},filter not strcmp={\boot}{}]{Tab_2.csv}{5=\expa,6=\rec,7=\dri,8=\boot}{\expa}}
\newcommand{\datad}{\csvreader[separator=comma, no head, tabular={c},filter not strcmp={\boot}{}]{Tab_2.csv}{5=\expa,6=\rec,7=\dri,8=\boot}{\rec}}
\newcommand{\datae}{\csvreader[separator=comma, no head, tabular={c},filter not strcmp={\boot}{}]{Tab_2.csv}{5=\expa,6=\rec,7=\dri,8=\boot}{\dri}}
\newcommand{\dataf}{\csvreader[separator=comma, no head, tabular={c},filter not strcmp={\boot}{}]{Tab_2.csv}{5=\expa,6=\rec,7=\dri,8=\boot}{\boot}}

\begin{document}
\begin{table}
\centering
\begin{tabular}{ l c c c c c} 
\hline \hline
& & \multicolumn{2}{c}{Regime} & \multicolumn{2}{c}{Significance} \\
\cmidrule(lr){3-4} \cmidrule(l){5-6} 
Impact & Horizon & State1 & State2 & CI & Bootstrap\\
\hline
\dataa & \datab & \datac & \datad & \datae & \dataf \\
\hline
\end{tabular}
\end{table}

\end{document}

enter image description here

The csv file I'm using has this format (I can't change it, it's been handled to me like this):

,,,,,,,
,,,,,,,
,,,,,,,
,,,,-0.010782,-0.010116,0.48334,0.1108
,,,,-0.041457,-0.015253,0.1942,0.0297
,,,,-0.075416,-0.026916,0.11368,0.0127
,,,,-0.083706,-0.028817,0.11713,0.0259
,,,,0.0018477,-0.0098031,0.20488,0.4541
,,,,-0.0097454,-0.018515,0.38455,0.2622
,,,,-0.025355,-0.0038709,0.2999,0.2734
,,,,-0.042006,0.014067,0.1024,0.283

I have a look for similar problems, but none helped me, I've also been through the help file of the package csvsimple without luck. I also apologize if this issue has already been addressed.

Best Answer

The following code utilizes siunitx. It is based on this answer showing how to establish compatibility between siunitx and csvsimple.

table

\documentclass{article}

\usepackage{booktabs}
\usepackage{csvsimple}
\usepackage[round-mode=places, round-integer-to-decimal, round-precision=4,
    table-format = 1.4, 
    table-number-alignment=center,
    round-integer-to-decimal]{siunitx}
\newcolumntype{N}{c@{}S}

\newcommand{\dataa}{\begin{tabular}{c} \\  \\ \\ Var1 \\ \\  \\ \\Var2 \\  \end{tabular}}
\newcommand{\datab}{\begin{tabular}{c}  4\\ 8\\12\\16\\4\\ 8\\12\\16\\  \end{tabular}}
\newcommand{\datac}{\csvreader[separator=comma, no head, tabular={N},filter not strcmp={\boot}{}]{test.csv}{5=\expa,6=\rec,7=\dri,8=\boot}{& \expa}}
\newcommand{\datad}{\csvreader[separator=comma, no head, tabular={N},filter not strcmp={\boot}{}]{test.csv}{5=\expa,6=\rec,7=\dri,8=\boot}{& \rec}}
\newcommand{\datae}{\csvreader[separator=comma, no head, tabular={N},filter not strcmp={\boot}{}]{test.csv}{5=\expa,6=\rec,7=\dri,8=\boot}{& \dri}}
\newcommand{\dataf}{\csvreader[separator=comma, no head, tabular={N},filter not strcmp={\boot}{}]{test.csv}{5=\expa,6=\rec,7=\dri,8=\boot}{& \boot}}

\begin{document}
    \begin{table}
        \centering
        \begin{tabular}{ l c c c c c } 
            \toprule
            & & \multicolumn{2}{c}{Regime} & \multicolumn{2}{c}{Significance}\\\cmidrule(lr){3-4} \cmidrule(l){5-6} 
            Impact & Horizon & State1 & State2 & CI & Bootstrap\\\midrule
            \dataa & \datab & \datac & \datad & \datae & \dataf \\\bottomrule
        \end{tabular}
    \end{table}
\end{document}