为了漂亮地打印带有数字后缀的大数字,这解决方案运行良好,除了最后一个测试用例(发帖人已经指出了这一点,所以这并不意外)。令人惊讶的是,6260563123
被正确地缩短为6.3G
,但日志中还出现了以下消息:
我最多只能到 2147483647='17777777777="7FFFFFFF,所以我用那个数字代替你的
有人可以指导我如何解决这个问题吗?
答案1
这支持所有已知的前缀和相当大的数字。
\documentclass{article}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\postfix}{m}
{
\int_case:nnF { \str_count:n { #1 } }
{
{1}{#1}
{2}{#1}
{3}{#1}
{4}{\hs_print:nnn { #1 } { 3 } { k }}
{5}{\hs_print:nnn { #1 } { 3 } { k }}
{6}{\hs_print:nnn { #1 } { 3 } { k }}
{7}{\hs_print:nnn { #1 } { 6 } { M }}
{8}{\hs_print:nnn { #1 } { 6 } { M }}
{9}{\hs_print:nnn { #1 } { 6 } { M }}
{10}{\hs_print:nnn { #1 } { 9 } { G }}
{11}{\hs_print:nnn { #1 } { 9 } { G }}
{12}{\hs_print:nnn { #1 } { 9 } { G }}
{13}{\hs_print:nnn { #1 } { 12 } { T }}
{14}{\hs_print:nnn { #1 } { 12 } { T }}
{15}{\hs_print:nnn { #1 } { 12 } { T }}
{16}{\hs_print:nnn { #1 } { 15 } { P }}
{17}{\hs_print:nnn { #1 } { 15 } { P }}
{18}{\hs_print:nnn { #1 } { 15 } { P }}
{19}{\hs_print:nnn { #1 } { 18 } { E }}
{20}{\hs_print:nnn { #1 } { 18 } { E }}
{21}{\hs_print:nnn { #1 } { 18 } { E }}
{22}{\hs_print:nnn { #1 } { 21 } { Z }}
{23}{\hs_print:nnn { #1 } { 21 } { Z }}
{24}{\hs_print:nnn { #1 } { 21 } { Z }}
}
{\hs_print:nnn { #1 } { 24 } { Y }}
}
\cs_new:Nn \hs_print:nnn
{
\fp_eval:n { round(#1 / 10^(#2), 1) } #3
}
\ExplSyntaxOn
\newcommand\test[1]{\par#1 ${}\rightarrow{}$ \postfix{#1}}
\begin{document}
\test{6}
\test{62}
\test{626}
\test{6260}
\test{62605}
\test{626056}
\test{6260563}
\test{62605631}
\test{626056312}
\test{6260563123}
\test{62605631234}
\test{626056312345}
\test{6260563123456}
\test{62605631234567}
\test{626056312345678}
\test{6260563123456789}
\test{62605631234567890}
\test{626056312345678901}
\test{6260563123456789012}
\test{62605631234567890123}
\test{626056312345678901234}
\test{6260563123456789012345}
\test{62605631234567890123456}
\test{626056312345678901234567}
\test{6260563123456789012345678}
\test{62605631234567890123456789}
\test{626056312345678901234567890}
\test{6260563123456789012345678901}
\test{62605631234567890123456789012}
\test{626056312345678901234567890123}
\test{6260563123456789012345678901234}
\test{62605631234567890123456789012345}
\test{626056312345678901234567890123456}
\test{6260563123456789012345678901234567}
\test{62605631234567890123456789012345678}
\end{document}
答案2
这是我的可以处理大量数字的解决方案。
\documentclass{article}
\usepackage{xfp,xintfrac}
\def\zz#1{%
\xintifGt{#1}{1000000000}{
\fpeval{round(#1/1000000000, 1)}G%
}{\xintifGt{#1}{1000000}{
\fpeval{round(#1/1000000, 1)}M%
}{\xintifGt{#1}{1000}{
\fpeval{round(#1/1000, 1)}K%
}{
#1%
}}}}
\def\test#1{\par#1 $\rightarrow$ \zz{#1}}
\begin{document}
\test{626}
\test{6260}
\test{62605}
\test{626056}
\test{6260563}
\test{62605631}
\test{626056312}
\test{6260563123}
\end{document}