[Tex/LaTex] Alignment in tabularx environment

tabularx

I want to generate a table that looks approximately as follows:

I want to use tabularx to have equidistant cells for the cells that are below the second row and right of the first column.

desired table

Here is my Latex code:

\documentclass{article}

\usepackage{tabularx}
\usepackage{booktabs}

\begin{document}

\begin{tabularx}{.5\textwidth}{l *{6}{X} }
\toprule
 & \multicolumn{2}{c}{first} & \multicolumn{2}{c}{second} & \multicolumn{2}{c}{third} \\
\multicolumn{1}{c}{data set} & foo & bar & foo & bar & foo & bar \\
\midrule
d1 & 28.4 & 4.4 & 25.9 & 8.9 & -3.7 & 33.3 \\
d2 & 2.8 & 3.7 & 5.1 & 16.3 & 47.0 & 0.1 \\
d3 & 32.1 & 4.2 & 31.5 & 4.0 & 23.0 & 3.3 \\
\bottomrule
\end{tabularx}

\end{document}

This is generated by pdflatex:

table generated by latex

I need tabularx since I want to give the total width of the table and let tabularx (or a similar package) do the length calculation.
All numers should be aligned to the right and a number should not wrap between the minus and the letters.

Best Answer

Don't use tabularx on numeric data:-)

enter image description here

\documentclass{article}

\usepackage{dcolumn}
\usepackage{booktabs}

\begin{document}

\begin{center}
\begin{tabular}{l *{6}{D..{3.1}}}
\toprule
 & \multicolumn{2}{c}{first} & \multicolumn{2}{c}{second} & \multicolumn{2}{c}{third} \\
\multicolumn{1}{c}{data set} & 
\multicolumn{1}{c}{foo} &
\multicolumn{1}{c}{bar} &
\multicolumn{1}{c}{foo} &
\multicolumn{1}{c}{bar} &
\multicolumn{1}{c}{foo} &
\multicolumn{1}{c}{bar} \\
\midrule
d1 & 28.4 & 4.4 & 25.9 & 8.9 & -3.7 & 33.3 \\
d2 & 2.8 & 3.7 & 5.1 & 16.3 & 47.0 & 0.1 \\
d3 & 32.1 & 4.2 & 31.5 & 4.0 & 23.0 & 3.3 \\
\bottomrule
\end{tabular}
\end{center}

\end{document}