我有这个PHP代码将数字转换为单词:
<?php
class NumericHelper {
public static function ToWord($number) {
$digit1 = array (
0 => 'zero',
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five',
6 => 'six',
7 => 'seven',
8 => 'eight',
9 => 'nine'
);
$digit1_5 = array (
1 => 'eleven',
2 => 'twelve',
3 => 'thirteen',
4 => 'fourteen',
5 => 'fifteen',
6 => 'sixteen',
7 => 'seventeen',
8 => 'eighteen',
9 => 'nineteen'
);
$digit2 = array (
1 => 'ten',
2 => 'twenty',
3 => 'thirty',
4 => 'forty',
5 => 'fifty',
6 => 'sixty',
7 => 'seventy',
8 => 'eighty',
9 => 'ninety'
);
$digit3 = array (
1 => 'one hundred',
2 => 'two hundred',
3 => 'three hundred',
4 => 'four hundred',
5 => 'five hundred',
6 => 'six hundred',
7 => 'seven hundred',
8 => 'eight hundred',
9 => 'nine hundred'
);
$steps = array (
1 => 'thousand',
2 => 'million',
3 => 'billion',
4 => 'trillion',
5 => 'quadrillion',
6 => 'quintillion',
7 => 'sextillion',
8 => 'septillion',
9 => 'octillion',
10 => 'nonillion',
11 => 'decillion'
);
$t = array (
'and' => 'and'
);
return self::toWord2 ( $number, $digit1, $digit1_5, $digit2, $digit3, $t );
}
private function toWord2($number, array $digit1, array $digit1_5, array $digit2, array $digit3, array $t) {
$formated = self::numberFormat ( $number, 0, '.', ',' );
$groups = explode ( ',', $formated );
$steps = count ( $groups );
$parts = array ();
foreach ( $groups as $step => $group ) {
$group_words = self::groupToWords ( $group, $digit1, $digit1_5, $digit2, $digit3 );
if ($group_words) {
$part = implode ( ' ' . $t ['and'] . ' ', $group_words );
if (isset ( $steps [$steps - $step - 1] )) {
$part .= ' ' . $steps [$steps - $step - 1];
}
$parts [] = $part;
}
}
return implode ( ' ' . $t ['and'] . ' ', $parts );
}
private static function numberFormat($number, $decimal_precision = 0, $decimals_separator = '.', $thousands_separator = ',') {
$number = explode ( '.', str_replace ( ' ', '', $number ) );
$number [0] = str_split ( strrev ( $number [0] ), 3 );
$total_segments = count ( $number [0] );
for($i = 0; $i < $total_segments; $i ++) {
$number [0] [$i] = strrev ( $number [0] [$i] );
}
$number [0] = implode ( $thousands_separator, array_reverse ( $number [0] ) );
if (! empty ( $number [1] )) {
$number [1] = Round ( $number [1], $decimal_precision );
}
return implode ( $decimals_separator, $number );
}
private static function groupToWords($group, array $digit1, array $digit1_5, array $digit2, array $digit3) {
$d3 = floor ( $group / 100 );
$d2 = floor ( ($group - $d3 * 100) / 10 );
$d1 = $group - $d3 * 100 - $d2 * 10;
$group_array = array ();
if ($d3 != 0) {
$group_array [] = $digit3 [$d3];
}
if ($d2 == 1 && $d1 != 0) { // 11-19
$group_array [] = $digit1_5 [$d1];
} else if ($d2 != 0 && $d1 == 0) { // 10-20-...-90
$group_array [] = $digit2 [$d2];
} else if ($d2 == 0 && $d1 == 0) { // 00
} else if ($d2 == 0 && $d1 != 0) { // 1-9
$group_array [] = $digit1 [$d1];
} else { // Others
$group_array [] = $digit2 [$d2];
$group_array [] = $digit1 [$d1];
}
if (! count ( $group_array )) {
return FALSE;
}
return $group_array;
}
}
?>
它的工作方式如下:echos 54,687,634 as:五十四,六百八十七,六百三十四
但我希望这样展示:fifty X four million and six hundred X eighty X seven thousand and six hundred X thirty X four
答案 0 :(得分:1)
function convert_number_to_words($number) {
$hyphen = '-';
$conjunction = ' and ';
$separator = ', ';
$negative = 'negative ';
$decimal = ' point ';
$dictionary = array(
0 => 'zero',
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five',
6 => 'six',
7 => 'seven',
8 => 'eight',
9 => 'nine',
10 => 'ten',
11 => 'eleven',
12 => 'twelve',
13 => 'thirteen',
14 => 'fourteen',
15 => 'fifteen',
16 => 'sixteen',
17 => 'seventeen',
18 => 'eighteen',
19 => 'nineteen',
20 => 'twenty',
30 => 'thirty',
40 => 'fourty',
50 => 'fifty',
60 => 'sixty',
70 => 'seventy',
80 => 'eighty',
90 => 'ninety',
100 => 'hundred',
1000 => 'thousand',
1000000 => 'million',
1000000000 => 'billion',
1000000000000 => 'trillion',
1000000000000000 => 'quadrillion',
1000000000000000000 => 'quintillion'
);
if (!is_numeric($number)) {
return false;
}
if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
// overflow
trigger_error(
'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
E_USER_WARNING
);
return false;
}
if ($number < 0) {
return $negative . convert_number_to_words(abs($number));
}
$string = $fraction = null;
if (strpos($number, '.') !== false) {
list($number, $fraction) = explode('.', $number);
}
switch (true) {
case $number < 21:
$string = $dictionary[$number];
break;
case $number < 100:
$tens = ((int) ($number / 10)) * 10;
$units = $number % 10;
$string = $dictionary[$tens];
if ($units) {
$string .= $hyphen . $dictionary[$units];
}
break;
case $number < 1000:
$hundreds = $number / 100;
$remainder = $number % 100;
$string = $dictionary[$hundreds] . ' ' . $dictionary[100];
if ($remainder) {
$string .= $conjunction . convert_number_to_words($remainder);
}
break;
default:
$baseUnit = pow(1000, floor(log($number, 1000)));
$numBaseUnits = (int) ($number / $baseUnit);
$remainder = $number % $baseUnit;
$string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
if ($remainder) {
$string .= $remainder < 100 ? $conjunction : $separator;
$string .= convert_number_to_words($remainder);
}
break;
}
if (null !== $fraction && is_numeric($fraction)) {
$string .= $decimal;
$words = array();
foreach (str_split((string) $fraction) as $number) {
$words[] = $dictionary[$number];
}
$string .= implode(' ', $words);
}
return $string;}
答案 1 :(得分:-1)
`
$hyphen = '-';
$conjunction = ' and ';
$separator = ', ';
$negative = 'negative ';
$decimal = ' point ';
$dictionary = array(
0 => 'zero',
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five',
6 => 'six',
7 => 'seven',
8 => 'eight',
9 => 'nine',
10 => 'ten',
11 => 'eleven',
12 => 'twelve',
13 => 'thirteen',
14 => 'fourteen',
15 => 'fifteen',
16 => 'sixteen',
17 => 'seventeen',
18 => 'eighteen',
19 => 'nineteen',
20 => 'twenty',
30 => 'thirty',
40 => 'fourty',
50 => 'fifty',
60 => 'sixty',
70 => 'seventy',
80 => 'eighty',
90 => 'ninety',
100 => 'hundred',
1000 => 'thousand',
1000000 => 'million',
1000000000 => 'billion',
1000000000000 => 'trillion',
1000000000000000 => 'quadrillion',
1000000000000000000 => 'quintillion'
);
if (!is_numeric($number)) {
return false;
}
if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
// overflow
trigger_error(
'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
E_USER_WARNING
);
return false;
}
if ($number < 0) {
return $negative . convert_number_to_words(abs($number));
}
$string = $fraction = null;
if (strpos($number, '.') !== false) {
list($number, $fraction) = explode('.', $number);
}
switch (true) {
case $number < 21:
$string = $dictionary[$number];
break;
case $number < 100:
$tens = ((int) ($number / 10)) * 10;
$units = $number % 10;
$string = $dictionary[$tens];
if ($units) {
$string .= $hyphen . $dictionary[$units];
}
break;
case $number < 1000:
$hundreds = $number / 100;
$remainder = $number % 100;
$string = $dictionary[$hundreds] . ' ' . $dictionary[100];
if ($remainder) {
$string .= $conjunction . convert_number_to_words($remainder);
}
break;
default:
$baseUnit = pow(1000, floor(log($number, 1000)));
$numBaseUnits = (int) ($number / $baseUnit);
$remainder = $number % $baseUnit;
$string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
if ($remainder) {
$string .= $remainder < 100 ? $conjunction : $separator;
$string .= convert_number_to_words($remainder);
}
break;
}
if (null !== $fraction && is_numeric($fraction)) {
$string .= $decimal;
$words = array();
foreach (str_split((string) $fraction) as $number) {
$words[] = $dictionary[$number];
}
$string .= implode(' ', $words);
}
return $string;
}?>`
使用上面的代码,让我知道您的观点。 干杯:)