Php 把数字转换成单词

Php 把数字转换成单词,php,Php,下面的PHP代码将数字转换为文字。对于像5250这样的整数,这很好,但是,当给定一个带小数的数字时,它无法正确解析该数字 450将输出“450”。然而,450.5将输出“四千五百”,应该是四百五十点五 我在网上搜索过,但我找不到解决办法。有人能建议改正吗 <?php function inr($number){ //A function to convert numbers into words. $words = array( '0

下面的PHP代码将数字转换为文字。对于像5250这样的整数,这很好,但是,当给定一个带小数的数字时,它无法正确解析该数字

450将输出“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;

?>

问题中的代码没有将数字拆分为小数点;它只查看字符串中的所有数字。所以,它当然不起作用。要使其正常工作,请执行以下操作:

  • 使用上述程序处理小数点之前的零件
  • 如果有小数点:
    • 添加“点”
    • 例如,在小数点后每位数加一个单词,使
      .45
      变为
      第四点和第五点

    • 这里有一个可行的解决方案。它可能不完全适用于您正在尝试做的事情,但它应该是一个良好的开端。关键是通过一个递归调用重用您的工作。如果对小数点后的数字再次调用函数,则可以合并结果:

      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;
      
      函数inr($number,$doOtherWords=true){
      //将数字转换为文字的函数。
      $words=数组(
      “0”=>“,“1”=>“1”、“2”=>“2”、“3”=>“3”、“4”=>“4”、“5”=>“5”,
      ‘6’=>‘6’、‘7’=>‘7’、‘8’=>‘8’、‘9’=>‘9’、‘10’=>‘10’,
      ‘11’=>‘11’、‘12’=>‘12’、‘13’=>‘13’、‘14’=>‘fouteen’、‘15’=>‘十五’,
      ‘16’=>‘16’、‘17’=>‘17’、‘18’=>‘18’、‘19’=>‘19’、‘20’=>‘20’,
      ‘30’=>‘30’、‘40’=>‘40’、‘50’=>‘50’、‘60’=>‘60’、‘70’=>‘70’,
      ‘80’=>‘80’、‘90’=>‘90’);
      //首先找出数字的长度
      $number_length=strlen($number);
      //初始化空数组
      $number_array=数组(0,0,0,0,0,0,0,0);
      $received_number_array=array();
      //如果变量中有小数点,请保存。假设没有小数点
      $hasDecimal=false;
      $numberFrafterDecimal='';
      //将所有接收到的数字存储到一个数组中
      //只在找到小数点之前执行此操作
      
      对于($i=0;$i首先确定整数和小数点后的数字,请使用
      $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 */
      }
      
      可能重复的