Align numbers at the decimal point in LaTeX tables (siunitx S column)

The siunitx package adds the column type S. It puts the decimal markers of all numbers in the column below each other, whatever the number of digits before and after the marker.

Preamble

The snippets on this page need these lines before \begin{document}:

\usepackage{siunitx}
\usepackage{booktabs}

Use an S column

Write S in the column argument instead of r. Every cell that is not a number, such as the header, goes in braces.

\begin{tabular}{lS}
\toprule
Sample & {Mass} \\
\midrule
A & 3.14 \\
B & 12.5 \\
C & 120 \\
\bottomrule
\end{tabular}
Result
The compiled example

Open in the editor

Reserve space for the digits

table-format=3.2 reserves space for three digits before and two digits after the marker; the default is 2.2. Match it to the longest number in the column, with a leading minus sign if negative numbers occur: table-format=-3.2.

\begin{tabular}{lS[table-format=-3.2]}
\toprule
Sample & {Change} \\
\midrule
A & 3.14 \\
B & -12.5 \\
C & 120 \\
\bottomrule
\end{tabular}
Result
The compiled example

Open in the editor

Print a decimal comma

\sisetup{output-decimal-marker={,}} prints a comma. In the source you can type a point or a comma; siunitx reads both.

\sisetup{output-decimal-marker={,}}
\begin{tabular}{lS[table-format=3.2]}
\toprule
Sample & {Mass} \\
\midrule
A & 3.14 \\
B & 12,5 \\
C & 120 \\
\bottomrule
\end{tabular}
Result
The compiled example

Open in the editor

Round to a fixed number of decimals

round-mode=places with round-precision=2 rounds every number to two decimals and fills shorter numbers with zeros: 3.14159 becomes 3.14, and 2.5 becomes 2.50.

\begin{tabular}{lS[table-format=1.2, round-mode=places, round-precision=2]}
\toprule
Sample & {Value} \\
\midrule
A & 3.14159 \\
B & 2.5 \\
C & 1 \\
\bottomrule
\end{tabular}
Result
The compiled example

Open in the editor

Alternative: the dcolumn package

dcolumn defines the column type D{.}{,}{2}. The three arguments are the marker in the source, the marker in the output and the number of decimals. The cells are set in math mode, so a text header needs \multicolumn{1}{c}{...}.

Complete document:

\documentclass{article}
\usepackage{dcolumn}
\begin{document}
\begin{tabular}{lD{.}{,}{2}}
\hline
Sample & \multicolumn{1}{c}{Mass} \\
\hline
A & 3.14 \\
B & 12.5 \\
C & 120 \\
\hline
\end{tabular}
\end{document}
Result
The compiled example

Open in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages