# Floating point formatting in TeX

After long I was exposed to TeX and to a problem that seems to be so simple in traditional languages but surprisingly hard to solve in TeX.

We have system out of our scope of control that provides us with numeric inputs to TeX in the common US format, that is dot separator for fractional part and commas separating thousands. We'd like to do basic transformations using fp package but it doesn't really support this input. Also, using \numprint doesn't seem to be working for us.

Is there any easy way how we can strip comma thousand separators off the numbers in TeX before passing it to fp for further calculations? It's easy to transform the numbers back using numprint afterwards.

We could remove one comma but is there an easy way to remove them all?

-
Welcome to TeX.sx! Your question was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other, otherwise you won't be able to comment on or accept answers or edit your question. –  Torbjørn T. Nov 25 '11 at 11:29

## migrated from stackoverflow.comNov 25 '11 at 7:43

This question came from our site for professional and enthusiast programmers.

To remove all , from a string, you can use the following

\makeatletter
\newcommand{\removecommas}[1]
{\edef#1{\expandafter\removecommas@#1,\relax}}
\def\removecommas@#1,#2{%
#1%
\ifx#2\relax
\expandafter\removecommas@@
\fi
\removecommas@#2}
\def\removecommas@@#1#2{}
\makeatother

% example:
\def\MyFloatingPoint{1,000,000.00}
\removecommas{\MyFloatingPoint}
\typeout{\MyFloatingPoint} % prints "1000000.00" in the log


Another solution is to use the expl3 package.

\usepackage{expl3}
\def\MyFloatingPoint{1,000,000.00}
\ExplSyntaxOn
\tl_remove_all:Nn \MyFloatingPoint { , }
\ExplSyntaxOff
\typeout{\MyFloatingPoint} % prints "1000000.00" in the log


Here \ExplSyntaxOn and \ExplSyntaxOff are analogs of \makeatletter and \makeatother, which make _ and : into letters, and tell TeX to ignore spaces. The expl3 package also provides ways to manipulate floating point numbers, that could be an alternative to fp.

Also, you may be interested in using siunitx rather than numprint. It is somewhat more powerful (never used either of them, though).

-
As siunitx is designed for displaying numbers, it probably is not such a good choice here as the macros are not expandable. –  Joseph Wright Nov 25 '11 at 12:15
:%s/,//g