如何提取货币符号?

如何提取货币符号?

在 Datatool 包中,我们找到了货币数据类型。

货币数据类型是货币符号后跟整数或实数。

如何从货币数据中提取货币符号?

\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}

答案1

运行后\DTLifcurrency或内部等价物\@dtl@checknumerical,货币符号存储在\@dtl@currency,因此您可以使用

\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}

在此处输入图片描述

答案2

\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}

在此处输入图片描述

相关内容