在php中将数字转换为word不起作用 [英] convert number to word in php not working

查看:91
本文介绍了在php中将数字转换为word不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,以下PHP代码将数量转换为数量。这适用于整数(如5250),当它带有小数时会给出错误的结果。



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



我检查了分析过的网页,但无法纠正代码。任何机构都可以提出/更正代码?

 <?php 
函数inr($ number){
//将数字转换为单词的函数。
$ words = array(
'0'=>'','1'=>'one','2'=>'two','3'=>'three '','4'=>'四','5'=>'五',
'6'=>'六','7'=>'七','8'=' '''''''','10'='十','10','十' '13'=>'十三','14'=>'fouteen','15'=>'十五',
'16'=>'十六','17'=> '十七','18'=>'十八','19'=>'十九','20'=>'二十',
'30'=>'三十','40 '='fourty','50'=>'五十','60'=>'六十','70'=>'七十',
'80'=>八十','90'=>'ninty');

//首先找到数字的长度
$ number_length = strlen($ number);
//初始化一个空数组
$ number_array = array(0,0,0,0,0,0,0,0,0);
$ received_number_array = array();

//将所有收到的数字存储到数组
中($ i = 0; $ i <$ number_length; $ i ++){$ received_number_array [$ i] = substr($ number, $ I,1); }

//用接收的数字填充空数组 - 对于($ i = 9- $ number_length,$ j = 0; $ i <9; $ i ++, $ j ++){$ number_array [$ i] = $ received_number_array [$ j]; }
$ number_to_words_string =;
//找出是否是青少年?然后乘以10,例17是17,所以如果1乘以7乘以1乘以7并加上7。 ($ i = 0,$ j = 1; $ i <9; $ i ++,$ j ++){
if($ i == 0 || $ i == 2 || $ i如果($ number_array [$ i] ==1){
$ number_array [$ j] = 10 + $ number_array [$ j] ;
$ number_array [$ i] = 0;
}
}
}

$ value =; $ $ b $ 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 =对不起,这不支持超过99个金币; }
return ucwords(strtolower(Rupees。$ number_to_words_string)。Only。);

}


?>


解决方案

问题从这里开始:

  $ number_length = strlen($ number); 

考虑以下数字:

  1234 
12.3

这两个数字显然是不同的顺序但是由于你的代码围绕着字符串长度,所以它将这些数字视为相同的数量级。由于这个错误的假设,所返回的答案是不正确的。

为了解决这个问题,我们发现一个不同的系统被用来为小数前的数字创建单词值,而不是后。因此,您需要首先拆分小数点的数字,然后分别处理两个部分。

  $ integer_value = floor( $号); 

if(strstr($ number,'。')){
$ decimal_value =(int)substr($ number,strpos($ number,'。'));
//将$ decimal_value转换为它的单词值
}

经过测试的库可以为你做所有这些工作,所以除非这只是一个学习练习,否则我建议你使用其中的一个。


Please note, the following PHP code converts amount in number to amount in words. This works fine for integer like (ex. 5250) and when it with decimal it gives wrong results.

Ex: for 450 = four hundred fifty only. Ex: for 450.5 = four thousand five hundred five (which is wrong) and it should be four hundred fifty and fifty paisa only.

I have checked in the web analysed but unable to rectify the code. Can any body suggest /correct the code please?

<?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.");

    }


?>

解决方案

The problem starts here:

$number_length = strlen($number);

Consider the following numbers:

1234
12.3

The two numbers are clearly different orders of magnitude, but since your code centers around the string length, it treats the numbers as though they were the same order of magnitude. Because of this bad assumption, the answer returned is incorrect.

To fix the problem, recognize that a different system is used to create word values for the numbers before the decimals than after. For this reason, you need to first split the number at the decimal and process the two parts separately.

$integer_value = floor($number);

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

There are well-tested libraries that will do all of this for you, so unless this is just a learning exercise I suggest that you make use of one of those.

这篇关于在php中将数字转换为word不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆