如何在php中将数字转换为字符串(字母)?

时间:2014-09-26 11:56:55

标签: php php-5.3 php-5.5

输出应该是九千四百九十九。(除了没有开关(),     pow,日志功能。     尝试使用类函数。

/*Sample expected output*/
If input is 9499

examples: 
/*Input is 9499*/

/*Output is nine thousand four hundred ninety nine*/

1 个答案:

答案 0 :(得分:-1)

以下是示例....

<form method='POST' action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" >
Enter the Number:
<input type="number" max="999999999" name="number" required="required">(max value:999999999)<br/><input type="submit" value="Submit" name="Submit"><br/></form>
<?php
if(isset($_POST['Submit'])){
class N2L{
      var $number  = 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 => 'forty',50 => 'fifty',60 => 'sixty',70 => 'seventy',80 => 'eighty',90 => 'ninety',
                                                  100 => 'hundred',1000 => 'thousand',100000 => 'lakh',10000000 => 'crore');

      function index($numb){
          if($numb<100){
              return $this->upto_99($numb);
          }elseif($numb<1000){
              return $this->upto_999($numb);
          }elseif($numb<100000){
              return $this->upto_99999($numb);
          }elseif($numb<10000000){
              return $this->upto_9999999($numb);
          }else{
              return $this->upto_999999999($numb);
          }
      }
      function upto_999999999($numb){
          $number=  $this->number;
          if($numb<=200000000){
          $crores=(int)($numb/10000000);
          $str=" ".$number[$crores]." ".$number[10000000];
          }else{
              $crores=(int)($numb/100000000)*10;
              $crores_odd=(int)($numb%100000000)/10000000;
              $str=" ".$number[$crores]." ".$number[$crores_odd]." ".$number[10000000];
          }

          $tens_old=(int)($numb%10000000);
          $str.=" ".$this->upto_9999999($tens_old);

          return " ".$str;

      }

      function upto_9999999($numb){
          $number=  $this->number;
          if($numb<=2000000){
          $lakhs=(int)($numb/100000);
          $str=" ".$number[$lakhs]." ".$number[100000];
          }else{
              $lakhs=(int)($numb/1000000)*10;
              $lakhs_odd=(int)($numb%1000000)/100000;
              $str=" ".$number[$lakhs]." ".$number[$lakhs_odd]." ".$number[100000];
          }

          $tens_old=(int)($numb%100000);
          $str.=" ".$this->upto_99999($tens_old);

          return " ".$str;

      }
      function upto_99999($numb){
          $number=  $this->number;
          if($numb<=20000){
          $thousands=(int)($numb/1000);
          $str=" ".$number[$thousands]." ".$number[1000];
          }else{
              $thousands=(int)($numb/10000)*10;
              $thousands_odd=(int)($numb%10000)/1000;
              $str=" ".$number[$thousands]." ".$number[$thousands_odd]." ".$number[1000];
          }

          $tens_old=(int)($numb%1000);
          $str.=" ".$this->upto_999($tens_old);

          return " ".$str;

      }

      function upto_999($numb){
        $number=  $this->number;
        if($numb>99 && $numb<1000){
            $hundreds=(int)($numb/100);
            $str=" ".$number[$hundreds]." ".$number[100];
            $tens_old=(int)($numb%100);
            $str.=" ".$this->upto_99($tens_old);

            return " ".$str;
        }

        }
        function upto_99($numb){
            $number=  $this->number;
            if($numb>20 && $numb<100){
            $tens=(int)($numb/10)*10;
            $units=$numb%10;
            return $number[$tens]." ".$number[$units];
        }
            else{
            return " ".$number[$numb];
            }
        }
}
$leter=new N2L();

echo "The number ".$_POST['number']." in Words is ".$leter->index($_POST['number']);


}

?>