经过一番折腾,我想出了这个:
function numberEstimate($number) {
// Check for some special cases.
if ($number < 1) {
return "zero";
} else if ($number< 1000) {
return "less than 1 thousand";
}
// Define the string suffixes.
$sz = array("thousand", "million", "billion", "trillion", "gazillion");
// Calculate.
$factor = floor((strlen($number) - 1) / 3);
$number = floor(($number / pow(1000, $factor)));
$number = floor(($number / pow(10, strlen($number) - 1))) * pow(10, strlen($number) - 1);
return "over ".$number." ".@$sz[$factor - 1];
}
输出如下内容:
0 => "zero"
1 => "less than 1 thousand"
10 => "less than 1 thousand"
11 => "less than 1 thousand"
56 => "less than 1 thousand"
99 => "less than 1 thousand"
100 => "less than 1 thousand"
101 => "less than 1 thousand"
465 => "less than 1 thousand"
890 => "less than 1 thousand"
999 => "less than 1 thousand"
1,000 => "over 1 thousand"
1,001 => "over 1 thousand"
1,956 => "over 1 thousand"
56,123 => "over 50 thousand"
99,213 => "over 90 thousand"
168,000 => "over 100 thousand"
796,274 => "over 700 thousand"
999,999 => "over 900 thousand"
1,000,000 => "over 1 million"
1,000,001 => "over 1 million"
5,683,886 => "over 5 million"
56,973,083 => "over 50 million"
964,289,851 => "over 900 million"
769,767,890,753 => "over 700 billion"
7,687,647,652,973,863 => "over 7 gazillion"
这可能不是最漂亮的解决方案,也不是最优雅的解决方案,但它似乎工作并且做得很好,所以我可能会同意这个。
感谢大家的指点和建议!