[Tex/LaTex] Align table data by decimal points

tables

I have tried for about an hour and a half now to get my table data to align by the decimal places and I am beginning to lose my mind a bit. I am aware of the siunitx and dcolumn packages but cannot get either to work for my table. Could someone please help? Thanks! Below is the code for my table:

\begin{table}
    \begin{center}
    \begin{tabular}{p{2.3cm}p{2.3cm}p{2.3cm}p{2.3cm}p{2.3cm}p{1.5cm}} \hline \hline
        Radius \newline ($R/R_\odot$) & Luminosity \newline ($L/L_\odot$) & Mass \newline ($M/M_\odot$) & Temperature \newline ($10^6$ K) & Density \newline (kg/m$^3$) & Pressure \newline ($P/P_c$) \\ \hline
        0.0 & 0.00 & 0.00 & 15.5   & 160000  & 1.00 \\
        0.1 & 0.42 & 0.07 & 13.0   & 90000    & 0.46 \\
        0.2 & 0.94 & 0.35 & 9.5     & 40000    & 0.15 \\
        0.3 & 0.99 & 0.64 & 6.7     & 13000    & 0.04 \\
        0.4 & 0.99 & 0.85 & 4.8     & 4000      & 0.007 \\
        0.5 & 0.99 & 0.94 & 3.4     & 1000      & 0.001 \\
        0.6 & 0.99 & 0.98 & 2.2     & 400        & 0.0003 \\
        0.7 & 0.99 & 0.99 & 1.2     & 80          & 4$\times10^{-5}$ \\
        0.8 & 0.99 & 1.00 & 0.7     & 20          & 5$\times10^{-6}$ \\
        0.9 & 0.99 & 1.00 & 0.3     & 2            & 3$\times10^{-7}$\\
        1.0 & 0.99 & 1.00 & 0.006 & 0.0003   & 4$\times10^{-13}$ \\ \hline
    \end{tabular}
    \end{center}
\end{table}

And here is what the current table looks like. Note that the decimal points are not aligned (see Temperature column):
enter image description here

Best Answer

It's easy with siunitx:

\documentclass{article}
\usepackage{siunitx,booktabs}

\begin{document}

\begin{table}
\centering
\addtolength{\tabcolsep}{-0.3pt}
\begin{tabular}{
  @{}
  S[table-format=1.1]
  S[table-format=1.2]
  S[table-format=1.2]
  S[table-format=2.3]
  S[table-format=6.4,group-digits=integer,group-four-digits]
  S[table-format=1.2e-2]
  @{}
}
\toprule
{Radius} & {Luminosity} & {Mass} & {Temperature} & {Density} & {Pressure} \\
{($R/R_\odot$)} & {($L/L_\odot$)} & {($M/M_\odot$)} &
  {(\SI[parse-numbers=false]{10^6}{K})} & {(\si{kg/m^3})} & {($P/P_c$)} \\
\midrule
0.0 & 0.00 & 0.00 & 15.5  & 160000  & 1.00   \\
0.1 & 0.42 & 0.07 & 13.0  &  90000  & 0.46   \\
0.2 & 0.94 & 0.35 & 9.5   &  40000  & 0.15   \\
0.3 & 0.99 & 0.64 & 6.7   &  13000  & 0.04   \\
0.4 & 0.99 & 0.85 & 4.8   &   4000  & 0.007  \\
0.5 & 0.99 & 0.94 & 3.4   &   1000  & 0.001  \\
0.6 & 0.99 & 0.98 & 2.2   &    400  & 0.0003 \\
0.7 & 0.99 & 0.99 & 1.2   &     80  & 4e-5   \\
0.8 & 0.99 & 1.00 & 0.7   &     20  & 5e-6   \\
0.9 & 0.99 & 1.00 & 0.3   &      2  & 3e-7   \\
1.0 & 0.99 & 1.00 & 0.006 & 0.0003  & 4e-13  \\
\bottomrule
\end{tabular}

\end{table}

\end{document}

enter image description here

Tables with heterogeneous data are always difficult to typeset. If you don't want the space before the times sign in the last column, change the table preamble into

\begin{tabular}{
  @{}
  S[table-format=1.1]
  S[table-format=1.2]
  S[table-format=1.2]
  S[table-format=2.3]
  S[table-format=6.4,group-digits=integer,group-four-digits]
  S[table-format=1.0e-2,table-align-exponent=false]
  @{}
}

and the output will be

enter image description here

Related Question