![如何提取货币符号?](https://linux22.com/image/381037/%E5%A6%82%E4%BD%95%E6%8F%90%E5%8F%96%E8%B4%A7%E5%B8%81%E7%AC%A6%E5%8F%B7%EF%BC%9F.png)
在 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}