[Tex/LaTex] Convert string to number

macrosstrings

How can I convert numbers given as strings to "real" numbers? Consider the following MWE:

\documentclass{article}
\usepackage{datenumber}
\usepackage{xstring}
\begin{document}
\newcommand{\tag}{\StrBefore{01.01.2000}{.}}
%\setdatenumber{2000}{10}{\tag}
\setdatenumber{2000}{10}{10}
\tag \\
\datedate
\end{document}

Here, \tag is set to 01 as can be seen in the output. But if I use the line \setdatenumber{2000}{10}{\tag} I get the error "missing number, treated as zero". How can I avoid this? None of the other "Convert string to number" I found questions seem to be of help here.

Best Answer

The problem is caused by \StrBefore not being expandable, so the number is printed but it can not be used for further calculations. The xstring author added a optional argument to \StrBefore to help with this kind of problems: You can pass the name of a control sequence in [] to set this control sequence to the result of \StrBefore:

\documentclass{article}
\usepackage{datenumber}
\usepackage{xstring}
\begin{document}
\StrBefore{01.01.2000}{.}[\tag]
\setdatenumber{2000}{10}{\tag}
\tag \\
\datedate
\end{document}
Related Question