Rounding of decimal numbers: Looking for an elegant way in latex table

That could be done by »siunitx«, which will also align the digits at the decimal marker.

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{caption}
\usepackage{booktabs}  % nice looking tables
\usepackage{siunitx}

\begin{document}
  \begin{table}[!htb]
    \sisetup{round-mode=places}
    \caption{Table caption}
    \label{tab:default}
    \centering
    \begin{tabular}{
      S[round-precision=2]
      S[round-precision=3]
    }\toprule
      1.98185942 & 0.14495331 \\ \bottomrule
    \end{tabular}
  \end{table}
\end{document}

The alignment of the digits is centered by default. The package manual shows you options for left or right alignment.


enter image description here


You could use pgfplotstable. You can pass the values directly as macro argument, or, since you are not rounding the values by hand (it seems to me that they are a kind of a measurement series), you can also load them from a csv or similar formated file.

\documentclass{article}
\usepackage{pgfplotstable}

\begin{document}
    \pgfplotstabletypeset[
        %col sep=&,row sep=\\, % to immitate the tabular
                              % the default is: space = next column
                              %                 newline = next row
        % global:
        precision=5,          % five digits
        % only for the first row:
        columns/0/.style={precision=3},
        dec sep align,        % align at the dec point, default is centering
        header=false,         % input data has no header
        every head row/.style={output empty row} % output has no header
        ]
    { % here could also be a filename with the following content
        1.98185942  0.14495331
        1.9         0.14
        19.8        14.49
    }

\end{document}

If you have more tables like that you can also define a style for them so you do not have to copy and paste everything:

\pgfplotstableset{my style/.style={precision=5,
            columns/0/.style={precision=3},
            dec sep align, header=false,
            every head row/.style={output empty row}}
        }
\pgfplotstabletypeset[my style]
{   %
    1.98185942  0.14495331
    1.9         0.14
    19.8        14.49
}

There a many more options! Simply take a look into the manual to adjust the solution to your needs.

enter image description here

Tags:

Tables