Within the Datatool package, we find the currency data type.

A currency data type is a currency symbol followed by an integer or real number.

How can I extract the currency symbol out of a currency data ??

\documentclass{article}

\usepackage{datatool}

\newcommand{\splitCurrency}[1]{%
    \noindent Splitting #1 into its value and symbol parts:
    \DTLifcurrency{#1}% Check: Is #1 currency?
    {% Check: #1 is currency !
        \\The value: \DTLconverttodecimal{#1}{\theValue} \theValue%
        \\The symbol: ??
    }{% Check: #1 is not currency !
        Not a currency !
    }%
}

\begin{document}

\splitCurrency{\$ 123}%

\end{document}
up vote 5 down vote accepted

After running \DTLifcurrency or the internal equivalent \@dtl@checknumerical, the currency symbol is stored in \@dtl@currency, so you can use

\documentclass{article}

\usepackage{datatool}

\makeatletter
\newcommand\getcurrencysymbol[2]{%
  \@dtl@checknumerical{#1}%
  \ifnum\@dtl@datatype=3\relax\let#2\@dtl@currency\else\def#2{??}\fi
}
\makeatother
\newcommand{\splitCurrency}[1]{%
    \noindent Splitting #1 into its value and symbol parts:
    \DTLifcurrency{#1}% Check: Is #1 currency?
    {% Check: #1 is currency !
        \\The value: \DTLconverttodecimal{#1}{\theValue}\theValue%
        \\The symbol: \getcurrencysymbol{#1}{\theCurrency}\theCurrency
    }{% Check: #1 is not currency !
        Not a currency !
    }%
}

\begin{document}

\splitCurrency{\$ 123}%

\end{document}

enter image description here

  • Anyway to force the extension within \getcurrencysymbol, so that I don't have to repeat \expandafter\theCurrency\relax on each use? – Joseph Marie Aug 16 at 10:52
  • @JosephMarie I do not understand your question. Why would you have to repeat \expandafter\theCurrency\relax? \expandafter\theCurrency\relax would try to expand the non-expandable \relax, so \expandafter is useless here. – Marcel Krüger Aug 16 at 11:05
  • When I run \getcurrencysymbol{\$ 123}{\symbol}% \DTLnewdbentry{data}{Symbol}{\symbol}%, I get an Error (Extra \else....). When I do \DTLnewdbentry{data}{Symbol}{\expandafter\symbol\relax}%, there is no error. – Joseph Marie Aug 16 at 12:18
  • 2
    @JosephMarie that's imho a bug in datatool, it fails if the value starts with something that could be a currency. Your \expandafter command hides this. You could also use {\relax\symbol}. I wrote Nicola a mail. – Ulrike Fischer Aug 16 at 14:28
\documentclass{article}
\usepackage{datatool}
\DTLnewcurrencysymbol{€}

\def\splitCurrency#1{\noindent Splitting #1 into its value and symbol parts: 
    \splitCurrencyA#1;}
\def\splitCurrencyA#1#2;{%
    \DTLifcurrency{#1#2}% Check: Is #1 currency?
    {% Check: #1 is currency !
        \\The value: \DTLconverttodecimal{#2}{\theValue} \theValue%
        \\The symbol: #1
    }{% Check: #1 is not currency !
        Not a currency !
    }%
}

\begin{document}
    \splitCurrency{\$123}%

    \splitCurrency{€123}%
\end{document}

enter image description here

  • Very elegant solution! How does the argument parsing system knowns that he has to split into "text-value" at \def\splitCurrencyA#1#2; ? – Joseph Marie Aug 16 at 10:15
  • The currency symbol is only one character or a macro (#1) and the rest until the ; will be the value (#2), that‘s all. – Herbert Aug 16 at 11:36

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.