我想用文字表达一个有小数部分的值。我使用numberstringnum,没有小数也能正常工作,有谁能帮我吗?
\documentclass[12pt,a4paper,times,hidelinks,unicode]{report}
\usepackage{fmtcount}
\begin{document}
\numberstringnum{985.25}
\end{document}
答案1
\documentclass[12pt,a4paper]{report}
\usepackage{fmtcount}
\makeatletter
\def\foo#1{\xfoo#1..\relax}
\def\xfoo#1.#2.#3\relax{%
\numberstringnum{#1}%
\if$#2$\else
\space point%
% .10 -> point one zero
\@tfor\tmp:=#2\do{ \numberstringnum{\tmp}}%
% .10 -> point ten % if you _really_ need this
%
\fi
}
\makeatother
\begin{document}
\foo{985}
\foo{985.25}
\end{document}
babel 感知版本:
\documentclass[12pt,a4paper]{report}
\usepackage{fmtcount}
\usepackage[english,ngerman]{babel}
\makeatletter
\def\foo#1{\xxfoo#1,,\relax}
\def\xxfoo#1,#2,{\xfoo#1.#2.}
\def\xfoo#1.#2.#3\relax{%
\numberstringnum{#1}%
\if$#2$\else
\space\pointname
% .10 -> point one zero
\@tfor\tmp:=#2\do{ \numberstringnum{\tmp}}%
% .10 -> point ten % if you _really_ need this
% \numberstringnum{#2}%
\fi
}
\def\pointname{point}
\addto\extrasngerman{\def\pointname{Komma}}
\addto\extrasenglish{\def\pointname{point}}
\makeatother
\begin{document}
\foo{985}
\foo{985.25}
\foo{985,25}
\selectlanguage{english}
\foo{985}
\foo{985.25}
\foo{985,25}
\end{document}
答案2
我不确定这种做法是正式的还是非正式的,但在某些国家,以两位为一组来读小数似乎很常见。所以,我认为以下内容可能会有用:
\documentclass[12pt,a4paper]{report}
\usepackage{fmtcount}
\usepackage[english, ngerman, spanish]{babel}
\ExplSyntaxOn
\prop_new:N \l_floattostring_settings_prop
\NewDocumentCommand { \floattostringlocals } { m } {
\prop_set_from_keyval:Nn \l_floattostring_settings_prop {
decimal~separator = point,
word~separator = \space,
group~decimals = 1
}
\prop_put_from_keyval:Nn \l_floattostring_settings_prop { #1 }
}
\addto\extrasenglish{ \floattostringlocals { } }
\cs_new_protected:Nn \floattostring_regex_extractdigitgroups:nnN {
\regex_extract_all:nnN { \d{#1} | \d+ \z } { #2 } #3
}
\cs_generate_variant:Nn \floattostring_regex_extractdigitgroups:nnN { VV }
\seq_new:N \l_floattostring_parts_seq
\tl_new:N \l_floattostring_integer_tl
\tl_new:N \l_floattostring_decimals_tl
\int_new:N \l_floattostring_groupdecimals_int
\seq_new:N \l_floattostring_decimalsgroups_seq
\NewDocumentCommand { \floattostring } { m } {
\seq_clear:N \l_floattostring_parts_seq
\regex_split:nnN { \. | \, } { #1 } \l_floattostring_parts_seq
\seq_get_left:NN \l_floattostring_parts_seq \l_floattostring_integer_tl
\numberstringnum{ \l_floattostring_integer_tl }
\int_compare:nNnTF { \seq_count:N \l_floattostring_parts_seq } > { 1 } {
\prop_item:Nn \l_floattostring_settings_prop { word~separator }
\prop_item:Nn \l_floattostring_settings_prop { decimal~separator }
\seq_get_right:NN \l_floattostring_parts_seq \l_floattostring_decimals_tl
\int_set:Nn \l_floattostring_groupdecimals_int
{ \prop_item:Nn \l_floattostring_settings_prop { group~decimals } }
\floattostring_regex_extractdigitgroups:VVN
\l_floattostring_groupdecimals_int
\l_floattostring_decimals_tl
\l_floattostring_decimalsgroups_seq
\seq_map_inline:Nn \l_floattostring_decimalsgroups_seq {
\prop_item:Nn \l_floattostring_settings_prop { word~separator }
\numberstringnum{ ##1 }
}
} { }
}
\ExplSyntaxOff
\addto\extrasngerman{\floattostringlocals{ decimal separator = Komma }}
\addto\extrasspanish{\floattostringlocals{ decimal separator = punto, group decimals = 2 }}
\begin{document}
\selectlanguage{ngerman}
\floattostring{345.2345}
\floattostring{1.234}
\selectlanguage{spanish}
\floattostring{345.2345}
\floattostring{1.234}
\selectlanguage{english}
\floattostring{345.2345}
\floattostring{1.234}
\floattostringlocals{ group decimals = 2 }
\floattostring{345.2345}
\end{document}
该宏\floattostringlocals
将一个键值列表作为参数,该列表可能包含键decimal separator
、word separator
和group decimals
。键decimal separator
将应打印的小数分隔符的相关字符串作为值。使用键word separator
,可以切换到除空格之外的另一个字符串来分隔单词,因为某些语言不使用空格来分隔单词。最后,键group decimals
将一个整数作为值,该整数表示应如何对小数进行拼写分组。
结合\addto\extras<language>
可以为不同的语言设置不同的浮点数拼写规则。
上述代码将产生以下结果: