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 it possible to have automatic rounding of numbers in a table?

Say that I have this, made in the usual way with a tabular environment:

pi  | 3.14592654
e   | 2.7182818284
phi | 1.6180339887

Can I control how many significant digits are shown without truncating the numbers manually myself?

So for example, if I need four significant digits, the numbers should appear as

pi  | 3.1459
e   | 2.7183
phi | 1.6180

Bonus points for actually rounding the numbers according to the usual rules without simply truncating them.


I understand this is probably non-trivial, so here's a relaxed version that will also work in my circumstances (all numbers have one and the same precision and character count):

(How) Can I control how many characters are rendered in a column of a table?

So if I define the text in the columns as:

\begin{tabular} % some black magic column definition
   text & alongword\\
   text & anotherlongword\\
   text & morelongwords\\
\end{tabular}

I'd like to get something like:

text along
text anoth
text morel

I know there are non-TeX ways to do this, and I will probably resolve to writing a script if all else fails, but I'm curious whether there's a way to do it solely with TeX & friends.

share|improve this question
add comment

3 Answers

up vote 18 down vote accepted

For numbers:

There are some packages, like siunitx and math engine of tikz (especially in pgfplotstable), which can used to format numbers in LaTeX. numprint may be the simplest one, and can be easily used in tabulars.

numprint package privides n and N column types for numbers in tabular, and \nprounddigits for set the precision. An example:

% \usepackage{numprint} in preamble
\npdecimalsign{.}
\nprounddigits{2}
\begin{tabular}{c|n{5}{2}|}
foo & 12345 \\
bar & 12345.12345 \\
baz & 0.00012345 \\
\end{tabular}
\npnoround

Please read the manual for more options.


For common characters, @TH has showed some TeX trick. However, you can use xstring package or something else to do the dirty work. I don't know how to get full control in tabular, so here is just a incomplete solution, a mix of xtring and primitive TeX:

\halign{\StrLeft{#}{4} \cr
 abc \cr
 abcde \cr
 abcdefg \cr
}

Any further suggestions are welcome.

share|improve this answer
    
numprint is very nice! It has a lot other useful stuff that will certainly come in handy. Thanks! –  Martin Tapankov Jan 17 '11 at 12:37
    
How can I keep the header row text centered while using this package??? –  Daniel Aug 2 '13 at 21:08
    
@Daniel: For siunitx, use {header}. For other simpler packages, use \multicolumn{1}{c}{...} –  Leo Liu Aug 3 '13 at 5:07
add comment

Here's one way to do it that's a bit of a hack (which is to say that Leo Liu's answer is far better, it's just fun to try to do this yourself sometimes):

\documentclass{article}
\usepackage{array}
\usepackage{fp}
\newcolumntype{T}{>{\trunc}r}
\def\trunc\ignorespaces#1\\{%
        \FPset\a{#1}
        \FPround\a\a4
        \a
        \\
}
\begin{document}
\begin{tabular}{>{$}l<{$}T}
\pi  & 3.14159265358979323848\\
e    & 2.71828182845904523536\\
\phi & 1.61803399\\
x    & 37\\
\end{tabular}
\end{document}

alt text

The T specifier has to be at the end of the row and it must be terminated with a \\. You could write an analogous one that works in the middle of the row by delimiting it with & rather than \\.

The original version differed only in the definition of \trunc.

\def\trunc#1.#2#3#4#5#6\\{#1.#2#3#4#5\\}
share|improve this answer
    
@TH: It's a pity you deleted the first tricky answer (works for any characters). It's rather valuable, even only for fun. :) –  Leo Liu Jan 17 '11 at 12:14
    
@Leo: There we go, I added it back. –  TH. Jan 17 '11 at 12:22
add comment
\documentclass{article}
\usepackage{array,xstring}

\newcolumntype{N}{>{\GobbleA} r<{;;} }
\def\GobbleA\ignorespaces#1.#2;;{%
  \ignorespaces#1.\StrLeft{#2}{4}}

\newcolumntype{S}{>{\GobbleB} l }
\def\GobbleB#1#2\\{\StrLeft{#2}{4}\\}

\begin{document}
\begin{tabular}{>{$}l<{$} N S}
\pi  & 3.14159265358979323848 & delicious \\
e    & 2.71828182845904523536 & foo \\
\phi & 1.61803399             & extraordinarylong \\
x    & 37.11111               & { } \\
\end{tabular}

\end{document}

alt text

share|improve this answer
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.