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.

I want numbers like 5.5 million to be displayed using a comma as a thousands separator as "5,500,000". Is there a way to do this automatically, without having to manually put commas?

share|improve this question

5 Answers 5

up vote 26 down vote accepted

I think the siunitx package has this sort of facility.

\num[group-separator={,}]{1234567890}

Should give you 1,234,567,890

Also you can use this as a package option like so: \usepackage[group-separator={,}]{siunitx}

Be warned, this doesn't seem to work with older versions of siunitx

share|improve this answer
9  
[group-separator={,}] also works as package option. –  lockstep Nov 28 '10 at 18:13
    
I've just realised this doesn't work for me. Maybe I have an old version of siunitx or xkeyval siunitx.sty 2009/09/21 v1.3a A comprehensive (SI) units package xkeyval.sty 2008/08/13 v2.6a package option processing (HA) xkeyval.tex 2008/08/13 v2.6a key=value parser (HA) –  Seamus Nov 28 '10 at 18:22
    
Same here. I can't get any commas to appear even if I use group-separator as a package option. There is some problem with xkeyval. I am on Ubuntu Maverick and using its latest latex packages. Any ideas for fixes? –  donatello Nov 28 '10 at 18:56
1  
With siunitx v1.3, try [digitsep={,}]. –  lockstep Nov 28 '10 at 21:15
5  
Note also that in general giving options using \sisetup is a better approach than using the load-time options due to the issues with the LaTeX2e kernel and option expansion. –  Joseph Wright Nov 28 '10 at 21:28

Seamus' answer involving siunitx is the Right WayTM to do this. That said, it's not so hard to write a simple macro to pretty-print numbers.

\newcount\ppnum
\newcommand\ppnumber[1]{%
        \ppnum=#1\relax
        \ifnum\ppnum<0
                $-$%
                \ppnum=-\ppnum
        \fi
        \let\pptemp\empty
        \loop\ifnum\ppnum>999
                \count255=\ppnum
                \divide\ppnum by1000
                \count255=\numexpr \count255 - 1000*\ppnum \relax
                \edef\pptemp{,\ifnum\count255<100 0\ifnum\count255<10 0\fi\fi
                             \the\count255 \pptemp}%
        \repeat
        \the\ppnum
        \pptemp
}

The first time I wrote this, I used a token register rather than \pptemp, but that required writing \expandafter\expandafter\expandafter{\expandafter... which just seemed excessive.

Edit:
Given that I've now had to fix two bugs in my code, maybe I shouldn't have claimed that it isn't so hard to do this. =)

share|improve this answer
    
+1 for simple programming algorithms written in pure TeX. And apparently TeX doesn't have an \abs macro. –  Martin Tapankov Nov 29 '10 at 15:21
    
@Martin: Well, I did use the e-TeX primitive \numexpr because it was slightly shorter. –  TH. Nov 30 '10 at 3:08
    
TH. Your Macro has a small problem: when the number is for example 1008110, as the number gets divided by 1000 the second time, the remainder is 8, and the result is printed as: 1,8,110 rather than 1,008,110. I am trying to find a fix to this since this will happen for any number that has a remainder from the division from 0-99. Leo –  Leo Dec 1 '10 at 23:15
    
@Leo: You're absolutely right. Thank you for pointing this out. It required only a minor fix. Still, it shows why the right way to do it is to use siunitx rather than roll one's own. –  TH. Dec 7 '10 at 7:40
    
@TH.: I wanted to add a comment, but those are only allowed to consist of about 600 characters, therefore I added another answer (see below). –  Stephen Sep 14 '11 at 18:12

Using the http://www.ctan.org/pkg/numprint package with \npthousandsep{,} might be an alternative (found after writing the other answer - Murphy's Law!).

Btw: Grouping numbers into pairs of two can be done with the http://www.ctan.org/pkg/telprint package.

share|improve this answer

The siunitx documentation, v2.3d, 2011/08/18, section 2 Installation, says:

"The package requires LaTeX3 support as provided in the l3kernel and l3packages bundles. [...] LaTeX3, and so siunitx, requires the e-TeX extensions: these are available on all modern TEX systems."

Whenever siunitx/LaTeX3/e-TeX are not available (because one is stuck with something other than a modern TeX system), TH.'s \ppnumber is a great help, if

\count255=\numexpr \count255 - 1000*\ppnum \relax

is replaced by something like (there is no \count254 temporary counter, this is just a pseudo-example!)

\count254 =\ppnum
\multiply\count254 by 1000
\advance\count255 by -\count254 \relax

. TH. said that he deliberately choose

"e-TeX primitive \numexpr because it was slightly shorter"

, but for an alternative to siunitx (in regard of pretty printing numbers) I would suggest going for the most compatible code (even if longer). Additionally, I would suggest replacing

 $-$%

with

\ensuremath{-}%

because \ppnumber might be used in math mode (where the first $ would end the math mode), and I would also suggest

replacing

\newcommand\ppnumber[1]{%

with

\DeclareRobustCommand{\ppnumber}[1]{%

because one never knows whether the command will be used in a way, so that it can break. Further I needed to place a \def\pptemp{} before \newcommand\ppnumber (or before \DeclareRobustCommand{\ppnumber}), otherwise I get a

! Undefined control sequence.
\ppnumber  ...t 253\relax \relax \fi \let \pptemp

As a last thought: \@bsphack \@esphack might be used (requiring \makeatletter and \makeatother, of course). As a very last thought: Replacing , by \thousandseparator and adding

\def\thousandseparator{,}
%\def\thousandseparator{.}
%\def\thousandseparator{\,}

would make it clear that there is flexibility in choosing the separator. (There are languages where the comma is used instead of the period as decimal separator, for example.)

In summary, I got

\makeatletter

\def\thousandseparator{,}
%\def\thousandseparator{.}
%\def\thousandseparator{\,}

\def\pptemp{}
\def\ppnumbersign{}

\DeclareRobustCommand{\ppnumber}[1]{%
\@bsphack
\count255=#1\relax
\ifnum\count255 <0
\def\ppnumbersign{\ensuremath{-}}%
\count255=-\count255\relax
\else
\def\ppnumbersign{\empty}%
\fi
\let\pptemp\empty
\loop\ifnum\count255>999
\edef\ppnc{\the\count255}\relax
\divide\count255 by 1000
\edef\ppna{\the\count255}\relax
\multiply\count255 by 1000
\advance\count255 by -\ppnc
\count255=-\count255\relax
\edef\pptemp{\thousandseparator
\ifnum\count255<100 0
  \ifnum\count255<10 0
  \fi
\fi
\the\count255\pptemp
}%
\count255=\ppna
\repeat
\@esphack%
\ppnumbersign\ppna\pptemp\relax%
}

\makeatother

About count and counter:

http://www.texdev.net/2009/11/17/tex-counts-and-latex-counters/.

share|improve this answer
1  
Spaces (and newlines) are ignored after control words (e.g., \relax, \empty, \pptemp, \fi). The comments following those can thus be removed. Note that \count255 is a temporary (by convention only), but \count254 is not. You should probably use \newcount if you want to use others. I don't understand why you have \relax\relax in a bunch of places. Numbers can be followed by an optional space token so, for example, \count255=\count253 followed by a newline is sufficient. (In fact, the = is optional too.) –  TH. Sep 19 '11 at 0:29
    
@TH.: >"Note that \count255 is a temporary (by convention only), but \count254 is not." Ups! Did not know that, thanks for the information. I changed the code accordingly. –  Stephen Sep 20 '11 at 18:09

sistyle can typeset a wide range of numbers with custom number separators, such as exponential numbers. For example, try:

% preamble
\usepackage{sistyle}
\SIthousandsep{,}

% document
\num{5500000} and \num{5.50e6} are equal.

5,500,000 and 5.50 × 106 are equal.

share|improve this answer

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.