如果我想将给定数字显示为序数,我会这样做:
<?php
// Needs 'php5-intl' package to be installed on Debian/Ubuntu
$set_format = numfmt_create( 'en_US', NumberFormatter::ORDINAL );
// '3' is displayed as '3rd'
echo numfmt_format( $set_format, 3 );
?>
但是如果我想使用 内置 PHP 函数/类将给定数字显示为单词形式(例如第一、第二、第三等)的序数 像 NumberFormatter
,我该怎么做?可能吗?
相关链接:
最佳答案
您想使用 SPELLOUT
格式样式,而不是 ORDINAL
。
下一个问题是如何告诉格式化程序使用您感兴趣的特定规则集;即 %spellout-ordinal
。这可以通过使用 setTextAttribute()
来完成.
示例
$formatter = new NumberFormatter('en_US', NumberFormatter::SPELLOUT);
$formatter->setTextAttribute(NumberFormatter::DEFAULT_RULESET,
"%spellout-ordinal");
for ($i = 1; $i <= 5; $i++) {
echo $formatter->format($i) . PHP_EOL;
}
输出
first
second
third
fourth
fifth
关于php - 使用 NumberFormatter PHP 类将数字显示为 'word form' 中的序数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19409413/