以下PHP代码将数字转换为书面文字。这适用于像5250这样的整数,但是,如果给出带小数的数字,则不能正确解析数字。
450将输出"四百五十"。然而,450.5将输出"四千五百五十。"它应该是四百五十五点。
我搜索了网络,但我无法提出解决方案。有人可以建议更正吗?
<?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.");
}
$my_fig=inr(450.5);
echo $my_fig;
?>
答案 0 :(得分:1)
问题中的代码不会在小数点上分割数字;它只是查看字符串中的所有数字。所以,当然它不起作用。为了使其正常工作:
.45
变为point four five
。答案 1 :(得分:0)
这是一个有效的解决方案。它可能无法完全满足您的尝试,但它应该是一个良好的开端。关键是通过一次递归调用重新使用您的工作。如果再次为小数点后的数字调用函数,则可以合并结果:
function inr($number, $doOtherWords = true){
//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();
// Save off if we have a decimal in a variable. Assume we don't
$hasDecimal = false;
$numberAfterDecimal = '';
//Store all received numbers into an array
//ONLY do this until we find a decimal
for($i=0;$i<$number_length;$i++) {
$character = substr($number,$i,1);
if ($character == '.') {
$hasDecimal = true;
$numberAfterDecimal = substr($number, $i+1);
$received_number_array[$i] = 0;
} elseif ($hasDecimal) {
$received_number_array[$i] = 0;
} else {
$received_number_array[$i] = $character;
}
}
//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 ($doOtherWords) {
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 "; }
}
}
// At this point you have the solution for everything BEFORE the decimal
// Now handle the decimal through recursion if in fact we have one
if ($hasDecimal) {
$number_to_words_string .= 'point ' . inr($numberAfterDecimal, false);
}
if($number_length>9){ $number_to_words_string = "Sorry This does not support more than 99 Crores"; }
if ($doOtherWords) {
return ucwords(strtolower("Rupees ".$number_to_words_string)."Only.");
}
// Don't add the other words on the recursive call
return ucwords(strtolower($number_to_words_string));
}
$my_fig=inr(450.56);
echo $my_fig;
答案 2 :(得分:0)
首先确定整数和小数点后的数字,使用$number=explode(".",$number);
将两个数字分成一个数组。
$ number [0]将是您的整数,$ number [1]将是小数点后的数字。
function inr($number[0]){
/* Your function for the whole number */
}
function inr2($number[1]){
/* Your function for the number after the decimal point */
}