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?
6 Answers
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
-
17
-
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)
– SeamusNov 28, 2010 at 18:22 -
2
-
6Note 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, 2010 at 21:28 -
12Also note that you need
group-minimum-digits={3}
if you want it to work for 4-digit numbers (e.g.,1000
), otherwise for me it only works for 5 or more digits. Mar 1, 2017 at 3:37
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. =)
-
+1 for simple programming algorithms written in pure TeX. And apparently TeX doesn't have an \abs macro. Nov 29, 2010 at 15:21
-
@Martin: Well, I did use the e-TeX primitive
\numexpr
because it was slightly shorter.– TH.Nov 30, 2010 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– LeoDec 1, 2010 at 23:15
-
1@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, 2010 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).– StephenSep 14, 2011 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.
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.
Note that sistyle
is (in principle) superseded by siunitx
(see accepted answer); however, sistyle
has maintenance-only support.
-
Can I ask how to make it work with numbers that are of length 4? For example
\num{1111}
doesn't work (wanted1,111
).– pogibasSep 19, 2020 at 16:09 -
@PoGibas good question. This decision is intentional for both
sistyle
andsiunitx
, but I'm not sure how to modify it. It could be a new question.– Mike TSep 20, 2020 at 23:30
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/.
-
1Spaces (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, 2011 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.– StephenSep 20, 2011 at 18:09
I usually only use this once in my document and don't want to bother with some additional package, so I found the following to be a very useful trick:
$50{\small,}000$
-
3Welcome to tex.stackexchange.com! The original poster asked for automatic ways of doing this, not manual. This is a manual solution. Jun 4, 2021 at 11:32
-
2