Codeigniter控制器到字转换器的数量

问题描述 投票:2回答:1

我正在尝试使用Codeigniter控制器将任何数字转换为单词。

我的控制器如下:

public function convert_number_to_words($number)
    {

    $hyphen = ' ';
    $conjunction = ' and ';
    $separator = ' ';
    $negative = 'negative ';
    $decimal = ' and Cents ';
    $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',
    );

    if (!is_numeric($number)) {
        return false;
    }

    if ($number < 0) {
        return $negative . $this->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 . $this->convert_number_to_words($remainder);
            }
            break;
        default:
            $baseUnit = pow(1000, floor(log($number, 1000)));
            $numBaseUnits = (int)($number / $baseUnit);
            $remainder = $number % $baseUnit;
            $string = $this->convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
            if ($remainder) {
                $string .= $remainder < 100 ? $conjunction : $separator;
                $string .= $this->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 printWord(){

$numberToWord = $this->Number_model->getNumber();

$this->amount_word = $this->convert_number_to_words($numberToWord ) . " Only ***";  

    }

名为“ Number_model”的模型正确返回数字位数。之后,应在视图中使用以下php代码将其打印出来。

<?=$this->amount_word?>

期望的结果

我需要在我的视图中使用<?=$this->amount_word?>将这个数字打印成单词。但它显示->仅***,没有以字为单位的转换数字。

我不明白我要怎么做。谁能帮我吗?

php codeigniter
1个回答
0
投票

由于示例中的代码使用硬编码数字,并且视图返回了连接字符串"Only ***",因此问题很可能是模型的函数$this->Number_model->getNumber();返回了数组。因此,您需要处理您的函数才能使用此数组中的值:

例如,如果数组(使用print_r($numberToWord)查看数组的外观是:]

阵列([0] => 1234)

我们可以使用:

$this->amount_word = $this->convert_number_to_words($numberToWord[0] )

评论更新:

数组看起来像:

阵列([0] => stdClass对象([fuel_qty] => 30))

并且要解决fuel_qty,我们需要像这样发送数组的对象值:

$this->convert_number_to_words($numberToWord[0]->fuel_qty)

其中[0]是数组索引,->fuel_qty是对象的键

© www.soinside.com 2019 - 2024. All rights reserved.