# Can you get a table to auto format numbers to show thousand separators?

I'm preparing financial statements in latex and it would be very convenient to be able to just enter the numbers into the table and it automatic knows where to add the commas in the output (see first table table in MWE for how I'd like to enter numbers and second table for how I'd like example output). I've just manually entered the year ends in the table below but you can ignore for solution as I have a command that will enter them so they won't be typed in numbers on the file I'm working on. Also in the example a negative figure is enclosed in brackets but that seems to be out of alignment with the rest of the figures in the table. Is there any way to improve this.

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}

\begin{document}

\section*{Income \& Expenditure Account}
\begin{table}[htb]
\centering
\begin{tabular}{p{8.25cm}crr}
& \textbf{Note} & \textbf{2016} & \textbf{2015} \\ \hline\\
Income & 1 & 100000 & 102000 \\ \cline{3-4}\\
Cost of Sales & 2 & (50000) & (45000) \\ \cline{3-4}
Gross Profit & & 50000 & 57000 \\
\end{tabular}
\end{table}

\begin{table}[htb]
\centering
\begin{tabular}{p{8.25cm}crr}
& \textbf{Note} & \textbf{2016} & \textbf{2015} \\ \hline\\
Income & 1 & 100,000 & 102,000 \\ \cline{3-4}\\
Cost of Sales & 2 & (50,000) & (45,000) \\ \cline{3-4}
Gross Profit & & 50,000 & 57,000 \\
\end{tabular}
\end{table}

\end{document}

• You can use \rlap{)} to push the ) into the margin between columns. – John Kormylo Oct 16 '16 at 19:21

A version using package siunitx:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{siunitx}
\sisetup{group-separator={,}}

\begin{document}

\section*{Income \& Expenditure Account}
\begin{table}[htb]
\centering
\begin{tabular}{
p{7.5cm}
c
*2{S[table-format=6.0, table-space-text-pre=(, table-space-text-post=)]}
}
& \textbf{Note} & {\textbf{2016}} & {\textbf{2015}} \\
\midrule
Income & 1 & 100000 & 102000 \\
\cmidrule{3-4}\\
Cost of Sales & 2 & {(}50000{)} & {(}45000{)} \\
\cmidrule{3-4}
Gross Profit & & 50000 & 57000 \\
\end{tabular}
\end{table}
\end{document}


• Plain numbers can be specified as requested.
• The numbers are automatically aligned at the decimal marker.
• Other entries in the same column can be protected from number parsing by curly braces (see table header). These entries are then centered.
• The entry with parentheses is more complex. The example adds them as pre and post text.
• The question is tagged (or was) with booktabs. Therefore the example uses the rules provided by the package.
• Can you explain the code that sets up the colums \begin{tabular}{ p{7.5cm} c *2{S[table-format=6.0, table-space-text-pre=(, table-space-text-post=)]}} – gman Oct 16 '16 at 13:47
• @gman - The first two columns are of type p and c, respectively -- same as in your code. Columns 3 and 4 are both of type S, a type provided by the siunitx package. Both S columns are associated with 3 options: table-format=6.0 means that up to 6 digits may occur before the decimal marker, and 0 digits after the decimal marker. table-space-text-pre=( and table-space-text-post=) inform LaTeX how much space to set aside for the (optional) opening and closing parentheses. Type texdoc siunitx at a command prompt to bring up the user guide of this package. – Mico Oct 16 '16 at 16:39