its*_*_me 5 php numbers ordinal number-formatting numberformatter
如果我想显示给定数字作为序号,我会这样做:
<?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 );
?>
Run Code Online (Sandbox Code Playgroud)
但是,如果我想显示一个给定的数字作为序号字的形式(如第一,第二,第三等)使用内置的PHP函数/类一样NumberFormatter,我该怎么办呢?可能吗?
相关链接:
sal*_*the 16
您希望使用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;
}
Run Code Online (Sandbox Code Playgroud)
产量
first
second
third
fourth
fifth
Run Code Online (Sandbox Code Playgroud)