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

Is there a package to do the rounding numbers for latex tables? For example this table.

\begin{table}[htdp]
\caption{default}
\begin{center}
\begin{tabular}{c|c}
+1.98185942 & +0.14495331 \\
\end{tabular}
\end{center}
\label{default}
\end{table}%


I hope it could be as simple as:

\begin{rounding}[7.5f]
\begin{table}[htdp]
\caption{default}
\begin{center}
\begin{tabular}{c|c}
+1.98185942 & +0.14495331 \\
\end{tabular}
\end{center}
\label{default}
\end{table}%
\end{rounding}


Something like that. Thanks

-
possible duplicate of Rounding numbers in a table / Truncating text in table –  Adam Liter Aug 2 '13 at 20:56
There is no right aligned function or centered function in numprint package???? What should I do then? –  Daniel Aug 2 '13 at 21:06

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.

-
Is it possible to give another example to have different precisions for the 1st cell and 2nd cell? Then I will accept it as answer. Thanks a lot! –  Daniel Aug 2 '13 at 21:19
@Daniel: I have updated my example accordingly. –  Thorsten Donig Aug 2 '13 at 21:23

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
]
{ % 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},