Number to words functions

in
This function useful for spell number to words.
from php.net refference functions
<?php
function num_2_words($num, $c=0) {
    $ZERO = 'zero';
    $MINUS = 'minus';
    $lowName = array(
         "", "one", "two", "three", "four", "five",
         "six", "seven", "eight", "nine", "ten",
         "eleven", "twelve", "thirteen", "fourteen", "fifteen",
         "sixteen", "seventeen", "eighteen", "nineteen");
 
    $tys = array(
         "", "", "twenty", "thirty", "forty", "fifty",
         "sixty", "seventy", "eighty", "ninety");
 
    $groupName = array(
         "", "hundred", "thousand", "million", "billion",
         "trillion", "quadrillion", "quintillion");
 
    $divisor = array(
         100, 10, 1000, 1000, 1000, 1000, 1000, 1000) ;
 
    $num = str_replace(",","",$num);
    $num = number_format($num,2,'.','');
    $cents = substr($num,strlen($num)-2,strlen($num)-1);
    $num = (int)$num;
 
    $s = "";
 
    if ( $num == 0 ) $s = $ZERO;
    $negative = ($num < 0 );
    if ( $negative ) $num = -$num;
    for ( $i=0; $num>0; $i++ ) {
       $remdr = (int)($num % $divisor[$i]);
       $num = $num / $divisor[$i];
       if ( $i == 1 /* doing hundreds */ && 1 <= $num && $num <= 5 ){
           if ( $remdr > 0 ){
               $remdr = ($num * 10);
               $num = 0;
           } // end if
       } // end if
       if ( $remdr == 0 ){
           continue;
       }
       $t = "";
       if ( $remdr < 20 ){
           $t = $lowName[$remdr];
       }
       else if ( $remdr < 100 ){
           $units = (int)$remdr % 10;
           $tens = (int)$remdr / 10;
           $t = $tys [$tens];
           if ( $units != 0 ){
               $t .= "-" . $lowName[$units];
           }
       }else {
           $t = num_2_words($remdr, 0);
       }
       $s = $t." ".$groupName[$i]." ".$s;
       $num = (int)$num;
    } // end for
    $s = trim($s);
    if ( $negative ){
       $s = $MINUS . " " . $s;
    }
 
    if ($c == 1) $s .= " and $cents/100";
 
    return $s;
}
 
$num1 = "128745";
$num2 = "432386.65";
 
echo $num1 .": ". num_2_words($num1)."<br/>";
echo $num2 .": ". num_2_words($num2)."<br/>";
echo $num2 .": ". num_2_words($num2,1)."<br/>";
 
?>
Usage :
<?php
 
echo num_2_words($number); 
echo num_2_words($number, 1) // show cents
 
?>



Example :


Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.