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.

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?

share|improve this question
    
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
add comment

migrated from stackoverflow.com Nov 25 '11 at 7:43

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

2 Answers

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).

share|improve this answer
    
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
add comment

You can open the TeX file in Vim and type the following:

:%s/,//g

This will find and replace any commas you have with nothing, thereby deleting them. The g at the end means "global", so if there are any commas you want left in, use cg instead of g to check before each comma.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.