在PHP中将数字转换为单词无效

时间:2014-02-21 04:50:56

标签: php html

请注意,以下PHP代码将数量转换为单词数量。这适用于整数(例如5250),当它带有十进制时,它会产生错误的结果。

例如: 450 =仅限四百五十。 例如: 450.5 =四千五百五(这是错误的),它应该只有四百五十五个paisa。

我已经在网上查了但是无法纠正代码。任何机构都可以建议/更正代码吗?

<?php
    function inr($number){
        //A function to convert numbers into words.
        $words = array(
        '0'=> '' ,'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' => 'fouteen','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' => 'ninty');

        //First find the length of the number
        $number_length = strlen($number);
        //Initialize an empty array
        $number_array = array(0,0,0,0,0,0,0,0,0);        
        $received_number_array = array();

        //Store all received numbers into an array
        for($i=0;$i<$number_length;$i++){    $received_number_array[$i] = substr($number,$i,1);    }

        //Populate the empty array with the numbers received - most critical operation
        for($i=9-$number_length,$j=0;$i<9;$i++,$j++){ $number_array[$i] = $received_number_array[$j]; }
        $number_to_words_string = "";        
        //Finding out whether it is teen ? and then multiplying by 10, example 17 is seventeen, so if 1 is preceeded with 7 multiply 1 by 10 and add 7 to it.
        for($i=0,$j=1;$i<9;$i++,$j++){
            if($i==0 || $i==2 || $i==4 || $i==7){
                if($number_array[$i]=="1"){
                    $number_array[$j] = 10+$number_array[$j];
                    $number_array[$i] = 0;
                }        
            }
        }

        $value = "";
        for($i=0;$i<9;$i++){
            if($i==0 || $i==2 || $i==4 || $i==7){    $value = $number_array[$i]*10; }
            else{ $value = $number_array[$i];    }            
            if($value!=0){ $number_to_words_string.= $words["$value"]." "; }
            if($i==1 && $value!=0){    $number_to_words_string.= "Crores "; }
            if($i==3 && $value!=0){    $number_to_words_string.= "Lakhs ";    }
            if($i==5 && $value!=0){    $number_to_words_string.= "Thousand "; }
            if($i==6 && $value!=0){    $number_to_words_string.= "Hundred "; }
        }
        if($number_length>9){ $number_to_words_string = "Sorry This does not support more than 99 Crores"; }
        return ucwords(strtolower("Rupees ".$number_to_words_string)." Only.");

    }


?>

3 个答案:

答案 0 :(得分:0)

你需要在小数点之后爆炸值,如

$numberarr = explode('.',$number);
        $number = $numberarr[0];
        $number_length = strlen($number);
//output Rupees Four Hundred Fifty Only.

还需要在$numberarr[1] //we get after (.) values

上开展更多工作

答案 1 :(得分:0)

为什么你写了$my_fig=inr(450.5)什么是inr()函数? 尝试使用intval()替换inr() ..

答案 2 :(得分:0)

问题从这里开始:

$number_length = strlen($number);

考虑以下数字:

1234
12.3

这两个数字显然是不同的数量级,但由于您的代码以字符串长度为中心,因此它将数字视为相同的数量级。由于这个错误的假设,返回的答案是不正确的。

要解决此问题,请注意使用不同的系统为小数点之前的数字创建单词值。因此,您需要先将小数字拆分并分别处理这两个部分。

$integer_value = floor($number);

if (strstr($number, '.')) {
    $decimal_value = (int)substr($number, strpos($number, '.'));
    // Convert $decimal_value to its word value
}

有经过充分测试的图书馆将为您完成所有这些,所以除非这只是一个学习练习,我建议您使用其中一个。