如何将数字转换为单词

时间:2015-03-03 06:24:26

标签: php loops

我正在尝试创建表单向导,因为我需要类进度 - 第一,进度 - 秒,进度 - 第三等等。 。

已从数据库中获取数据我已将此向导div置于循环中并且有一个变量$ i,其增量为1,2,3,4,5

所以我的问题是如何转换1 =第一,2 =第二,3 =第三,4 =第四,依此类推。 。

我发现他们正在转换的许多解决方案为1 = 1,2 = 2这样。 。

3 个答案:

答案 0 :(得分:3)

你能尝试一下。

function createFullWordOrdinal($number)
{
    $ord1     = array(1 => "first", 2 => "second", 3 => "third", 5 => "fifth", 8 => "eight", 9 => "ninth", 11 => "eleventh", 12 => "twelfth", 13 => "thirteenth", 14 => "fourteenth", 15 => "fifteenth", 16 => "sixteenth", 17 => "seventeenth", 18 => "eighteenth", 19 => "nineteenth");
    $num1     = array("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eightteen", "nineteen");
    $num10    = array("zero", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety");
    $places   = array(2 => "hundred", 3 => "thousand", 6 => "million", 9 => "billion", 12 => "trillion", 15 => "quadrillion", 18 => "quintillion", 21 => "sextillion", 24 => "septillion", 27 => "octillion");

    $number = array_reverse(str_split($number));

    if ($number[0] == 0) 
    {
        if ($number[1] >= 2)
            $out = str_replace("y", "ieth", $num10[$number[1]]);
        else
            $out = $num10[$number[1]]."th";
    } 
    else if (isset($number[1]) && $number[1] == 1) 
    {
        $out = $ord1[$number[1] . $number[0]];
    } 
    else 
    {
        if (array_key_exists($number[0], $ord1))
            $out = $ord1[$number[0]];
        else
            $out = $num1[$number[0]]."th";
    }

    if((isset($number[0]) && $number[0] == 0) || (isset($number[1]) && $number[1] == 1))
    {
        $i = 2;
    } 
    else 
    {
        $i = 1;
    }

    while ($i < count($number)) 
    {
        if ($i == 1) 
        {
            $out = $num10[$number[$i]] . " " . $out;
            $i++;
        } 
        else if ($i == 2) 
        {
            $out = $num1[$number[$i]] . " hundred " . $out;
            $i++;
        } 
        else 
        {
            if (isset($number[$i + 2])) 
            {
                $tmp = $num1[$number[$i + 2]] . " hundred ";
                $tmpnum = $number[$i + 1].$number[$i];
                if ($tmpnum < 20)
                    $tmp .= $num1[$tmpnum] . " " . $places[$i] . " ";
                else
                    $tmp .= $num10[$number[$i + 1]] . " " . $num1[$number[$i]] . " " . $places[$i] . " ";

                $out = $tmp . $out;
                $i+=3;
            } 
            else if (isset($number[$i + 1])) 
            {
                $tmpnum = $number[$i + 1].$number[$i];
                if ($tmpnum < 20)
                    $out = $num1[$tmpnum] . " " . $places[$i] . " " . $out;
                else
                    $out = $num10[$number[$i + 1]] . " " . $num1[$number[$i]] . " " . $places[$i] . " " . $out;
                $i+=2;
            } 
            else 
            {
                $out = $num1[$number[$i]] . " " . $places[$i] . " " . $out;
                $i++;
            }
        }
    }
    return $out;
}

这将为您提供所需的输出。

createFullWordOrdinal(1) ----> first
createFullWordOrdinal(2) ----> second
createFullWordOrdinal(3) ----> third
createFullWordOrdinal(4) ----> fourth

答案 1 :(得分:0)

如果你希望数字转换为,就像这样::

564:五百六十四

然后使用此功能将数字转换为

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;

}

<强>输出:

echo convert_number_to_words(123456789);

//一亿二千三百万,四十六万六千七百八十九

答案 2 :(得分:0)

  

转换1 =第一,2 =第二......尝试此功能

 function stringifyNumber($n) {
    $special = ['zeroth','first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth', 'eleventh', 'twelfth', 'thirteenth', 'fourteenth', 'fifteenth', 'sixteenth', 'seventeenth', 'eighteenth', 'nineteenth'];
    $deca = ['twent', 'thirt', 'fort', 'fift', 'sixt', 'sevent', 'eight', 'ninet'];
    if ($n < 20) return $special[$n];
        if ($n%10 === 0) return $deca[round($n/10,0)-2] . 'ieth';
        return $deca[round($n/10,0)-2] . 'y-' . $special[$n%10];
}



echo $number=stringifyNumber($number);