Format a number to human-readable text.
// Display: one thousand and twenty-four echo Text::number(1024);
// Display: five million, six hundred and thirty-two echo Text::number(5000632);
string Core_Text::number( integer $number )
参数列表
参数 类型 描述 默认值 $number
integer
Number to format
string
public static function number($number)
{
// The number must always be an integer
$number = (int)$number;
// Uncompiled text version
$text = array();
// Last matched unit within the loop
$last_unit = NULL;
// The last matched item within the loop
$last_item = '';
foreach (Text::$units as $unit => $name)
{
if ( $number / $unit >= 1 )
{
// $value = the number of times the number is divisble by unit
$number -= $unit * ($value = (int)floor($number / $unit));
// Temporary var for textifying the current unit
$item = '';
if ( $unit < 100 )
{
if ( $last_unit < 100 and $last_unit >= 20 )
{
$last_item .= '-' . $name;
}
else
{
$item = $name;
}
}
else
{
$item = Text::number($value) . ' ' . $name;
}
// In the situation that we need to make a composite number (i.e. twenty-three)
// then we need to modify the previous entry
if (empty($item))
{
array_pop($text);
$item = $last_item;
}
$last_item = $text[] = $item;
$last_unit = $unit;
}
}
if (count($text) > 1)
{
$and = array_pop($text);
}
$text = implode(', ', $text);
if ( isset($and) )
{
$text .= ' and ' . $and;
}
return $text;
}