# Replace . (period) by , (comma) in numbers

I pass a bunch of numbers into a custom command defined via \newcommand{\mycmd}[1]{...}:

\mycmd{1.12}
\mycmd{5.32}


\mycmd uses the number to draw a certain figure. On top of that, I would like to print the number in \mycmd, but with a comma instead of a decimal point: 1,12, 5,32.

Any suggestion would be appreciated.

-

I'm sure the siunitx package is the way to go because it will catch all corner cases. Out of curiosity, here is a simple solution that uses TeX's pattern matching capabilities.

\documentclass{minimal}
\def\usecomma#1.#2{#1,#2}
\newcommand\mycmd[1]{\usecomma #1}
\begin{document}
\obeylines
1.12 $\to$ \mycmd{1.12}
5.32 $\to$ \mycmd{5.32}
\end{document}


Note that it won't work if you pass an argument that has no dot, like \mycmd{2}. Is this too simple and there is something else that I have overlooked?

-
Good answer. My first idea was also to do it by hand like this, but then remembered siunitx. You could add some fall-back code to catch the case where no . is included. Also here #2 is not required, a simple \def\usecomma#1.{#1,} would be enough. –  Martin Scharrer Mar 21 '11 at 19:55
Why are you using \obeylines? –  Martin Scharrer Mar 21 '11 at 19:55
@Martin: thanks for your comments on my code and I like the idea just to match the first part including the dot. The \obeylines was just pure laziness on my part to quickly display examples line by line. (I should refrain from doing this in public.) I had no immediate idea for the fallback code - case selection is a part where I struggle a lot in TeX. –  Christian Lindig Mar 21 '11 at 20:05
Here some code which would catch the missing dot case. Then #1 ends in \empty which shouldn't be a problem and could be removed if so: \def\usecomma#1.#2\empty#3\relax{#1,#2} \newcommand\mycmd[1]{\usecomma #1\empty.\empty\relax} –  Martin Scharrer Mar 21 '11 at 20:08

This can be done using the siunitx package. Use the \num macro with the output-decimal-marker={,} option. The option can also be set globally using \sisetup{output-decimal-marker={,}}.

\documentclass{article}

\usepackage{siunitx}

\begin{document}

\num[output-decimal-marker={,}]{123.45}

\end{document}

-
Works fine in an isolated setting, thanks. But the siunitx package does not seem to work with the solution @Bruno Le Floch suggested in my question "Control order of columns in table": tex.stackexchange.com/questions/13907/… :( –  Frank Seifert Mar 21 '11 at 0:32
@Frank: Sorry, I can't see the connection. You would have to show how you use both together. –  Martin Scharrer Mar 21 '11 at 0:36
I take @Bruno Le Floch code and add \usepackage{siunitx} -> error. The problem probably goes beyond this question... –  Frank Seifert Mar 21 '11 at 0:44
@Frank: That's only because both define the s column character. Simply use a different one in Bruno's code and it will work. –  Martin Scharrer Mar 21 '11 at 0:53

There's a number of possible solutions; two of them: 1) Use the isonums package:

\documentclass{article}
\usepackage{isonums}
\newcommand\mycmd[1]{$#1$}

\begin{document}
\mycmd{5.32}
\end{document}


2) If you are using babel, you could use the spanish option and select it to typeset your number (the spanish option, by default uses the comma as the decimal separator):

\documentclass{article}
\usepackage[spanish,english]{babel}

\newcommand\mycmd[1]{$\selectlanguage{spanish}#1$}

\begin{document}
\mycmd{5.32}
\end{document}


Of course, if spanish is the only option for babel, then \selectlanguage{spanish} is superfluous.

-

You can just use the numprint package:

\documentclass{article}
\usepackage{numprint}

\begin{document}
\obeylines
1.12 $\to$ \numprint{1.12}
5.32 $\to$ \numprint{5.32}
\end{document}


-