# Table column align on decimal

Jeff Dean published this famous table of "numbers every software developer should know". I'm struggling to format it properly for a publication that is written in LaTeX.

As you can see, the left column is easy. It is left justified.

The right column needs to be aligned on the word "ns" with an occasional note after it. Also the first line has a decimal, while the other numbers are integers.

Here's my best attempt so far:

\begin{tabular}{ | l | r  |}
\hline
L1 cache reference                  &            0.5 ns       \\
Branch mispredict                   &            5 ns         \\
L2 cache reference                  &            7 ns         \\
Mutex lock/unlock                   &          100 ns (25)    \\
Main memory reference               &          100 ns         \\
Compress 1K bytes with Zippy        &       10,000 ns (3,000) \\
Send 2K bytes over 1 Gbps network   &       20,000 ns         \\
Read 1 MB sequentially from memory  &      250,000 ns         \\
Round trip within same datacenter   &      500,000 ns         \\
Disk seek                           &   10,000,000 ns         \\
Read 1 MB sequentially from network &   10,000,000 ns         \\
Read 1 MB sequentially from disk    &   30,000,000 ns (20,000,000) \\
Send packet CA to Netherlands to CA & 150,000,000 ns \\
\hline
\end{tabular}


My biggest concern is how to align the "ns". I'm less concerned with the 0.5 being formatted decimal aligned.

• If the units are always the same (ns), it would be better to move them into the column title, since the column of ns looks like a visual noise. Aug 23 '13 at 7:03

With siunitx, the unit ns is added via the column specification <, its space is reserved in the S column with the table-space-text-pst option. (It may be preferable to give the unit in the head and not in every row as it probably also applies to the parenthesized values, doesn’t it?)

In the last column the ( and ) are removed as input-open and input-close-uncertainty characters so that they can be used in the column freely.

## Code

\documentclass[varwidth,convert=false]{standalone}
\usepackage{booktabs}
\usepackage{siunitx}
\begin{document}
\begin{tabular}{
l
S[table-format=9.1, table-space-text-post={\,\si{\nano\second}}]
<{\,\si{\nano\second}}
S[table-format=8.0, table-space-text-pre=(, table-space-text-post=),
input-close-uncertainty={[}, input-open-uncertainty={]}]
}
\toprule
Description                         & \multicolumn{1}{c}{Some time} & {\footnotesize Other value} \\ \midrule
L1 cache reference                  & .5                            &  \\
Branch mispredict                   & 5                             &  \\
L2 cache reference                  & 7                             &  \\
Mutex lock/unlock                   & 100                           & (25)                        \\
Main memory reference               & 100                           &  \\
Compress 1K bytes with Zippy        & 10 000                        & (3 000)                     \\
Send 2K bytes over 1 Gbps network   & 20 000                        &  \\
Read 1 MB sequentially from memory  & 250 000                       &  \\
Round trip within same datacenter   & 500 000                       &  \\
Disk seek                           & 10 000 000                    &  \\
Read 1 MB sequentially from network & 10 000 000                    &  \\
Read 1 MB sequentially from disk    & 30 000 000                    & (20 000 000)                \\
Send packet CA to Netherlands to CA & 150 000 000                   &  \\ \bottomrule
\end{tabular}
\end{document}


## Output

One possibility using the dcolumn package:

\documentclass{article}
\usepackage{dcolumn}
\usepackage{amsmath}

\begin{document}

\begin{tabular}{ | l | D{;}{\text{\,ns\,}}{-1}  |}
\hline
L1 cache reference                  &            0.5 ;       \\
Branch mispredict                   &            5 ;         \\
L2 cache reference                  &            7 ;         \\
Mutex lock/unlock                   &          100 ; (25)    \\
Main memory reference               &          100 ;         \\
Compress 1K bytes with Zippy        &       10,000 ; (3,000) \\
Send 2K bytes over 1 Gbps network   &       20,000 ;         \\
Read 1 MB sequentially from memory  &      250,000 ;         \\
Round trip within same datacenter   &      500,000 ;         \\
Disk seek                           &   10,000,000 ;         \\
Read 1 MB sequentially from network &   10,000,000 ;         \\
Read 1 MB sequentially from disk    &   30,000,000 ; (20,000,000) \\
Send packet CA to Netherlands to CA & 150,000,000 ; \\
\hline
\end{tabular}

\end{document}


Perhaps the booktabs package might be of interest for you; you're tables will look much better, the use of vertical rules is discouraged.

The same table, using booktabs:

\documentclass{article}
\usepackage{booktabs}
\usepackage{dcolumn}
\usepackage{amsmath}

\begin{document}

\begin{tabular}{  l  D{;}{\text{\,ns\,}}{-1} }
\toprule
L1 cache reference                  &            0.5 ;       \\
Branch mispredict                   &            5 ;         \\
L2 cache reference                  &            7 ;         \\
Mutex lock/unlock                   &          100 ; (25)    \\
Main memory reference               &          100 ;         \\
Compress 1K bytes with Zippy        &       10,000 ; (3,000) \\
Send 2K bytes over 1 Gbps network   &       20,000 ;         \\
Read 1 MB sequentially from memory  &      250,000 ;         \\
Round trip within same datacenter   &      500,000 ;         \\
Disk seek                           &   10,000,000 ;         \\
Read 1 MB sequentially from network &   10,000,000 ;         \\
Read 1 MB sequentially from disk    &   30,000,000 ; (20,000,000) \\
Send packet CA to Netherlands to CA & 150,000,000 ; \\
\bottomrule
\end{tabular}

\end{document}