Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
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
add comment

2 Answers

up vote 16 down vote accepted

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

share|improve this answer
    
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
2  
@Daniel: I have updated my example accordingly. –  Thorsten Donig Aug 2 '13 at 21:23
add comment

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

share|improve this answer
    
A very interesting answer. –  Thorsten Donig Aug 2 '13 at 22:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.