将数字(1,2,3)转换为字符串(一,二,三)Cake PHP

时间:2015-02-18 09:45:52

标签: php cakephp

我是Cake环境的新手,我想将数字转换为我用来转换的函数。

var $helpers = array('NumberToWord');

3 个答案:

答案 0 :(得分:1)

您可以获得一个PEAR包来执行此操作:

http://pear.php.net/package-info.php?package=Numbers_Words

http://pear.php.net/package/Numbers_Words/docs/0.18.1/Numbers_Words/Numbers_Words.html#methodtoWords

$n2w = new Numbers_Words();
echo $n2w->toWords(5); // five

答案 1 :(得分:1)

使用此:

$num = 4263;

$numbers10 = array('ten','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety');
$numbers01 = array('one','two','three','four','fife','six','seven','eight','nine','ten',
    'eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen');

if($num == 0) {
    echo "zero";
}

$thousands = floor($num/1000);
if($thousands != 0) {
    echo $numbers01[$thousands-1] . " thousand ";
    $num -= $thousands*1000;
}

$hundreds = floor($num/100);
if($hundreds != 0) {
    echo $numbers01[$hundreds-1] . " hundred ";
    $num -= $hundreds*100;
}

if($num < 20) {
    if($num != 0) {
        echo $numbers01[$num-1];
    }
} else {
    $tens = floor($num/10);
    echo $numbers10[$tens-1] . " ";
    $num -= $tens*10;

    if($num != 0) {
        echo $numbers01[$num-1];
    }
}

输出:

  四千二百六十三

答案 2 :(得分:1)

CakePHP方式

  

第一步:创建一个类似于NumberComponent.php的组件文件

<?php
class NumberComponent extends Component
{
    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;
    }
}
?>
  

第二步:在控制器中调用

public function some_func() {
        // Loading Api components on the fly
        $this->NumberComponent = $this->Components->load ( 'Number' );
        $result = $this->NumberComponent->convert_number_to_words ( '123456789' );
        $result = $this->NumberComponent->convert_number_to_words ( '123456789123.12345' );

    }
  

输出1:

     

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

     

输出2:

     

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