Saturday, October 13, 2012

Convert numbers to words php

Convert numbers to words php

PHP Function convert a numbers to string of English words 

You can submit a number by using following form


<tbody><tr bgcolor="#ffffff"><td><form method="post" action="pw.php">
         <input name="num" type="text"></form></td><td></td></tr>
         <tr bgcolor="#ffffff">
 <td><input value="number in words" type="submit"></td><td></td></tr></tbody>
------------------------------------------------------------------------------------------------------------------------------
Returns value as follows
1000000 = one million
Back to try

<?php

$nwords = array(  "", "one", "two", "three", "four", "five", "six",
                "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
                "fourteen", "fifteen", "sixteen", "seventeen", "eightteen",
               "nineteen", "twenty", 30 => "thirty", 40 => "fourty",
                     50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eigthy",
                     90 => "ninety" );

function number_to_words ($x)
{
     global $nwords;
     if(!is_numeric($x))
     {
         $w = '#';
     }else if(fmod($x, 1) != 0)
     {
         $w = '#';
     }else{
         if($x < 0)
         {
             $w = 'minus ';
             $x = -$x;
         }else{
             $w = '';
         }
         if($x < 21)
         {
             $w .= $nwords[$x];
         }else if($x < 100)
         {
             $w .= $nwords[10 * floor($x/10)];
             $r = fmod($x, 10);
             if($r > 0)
             {
                 $w .= ' '. $nwords[$r];
             }
         } else if($x < 1000)
         {
      
                 $w .= $nwords[floor($x/100)] .' hundred';
             $r = fmod($x, 100);
             if($r > 0)
             {
                 $w .= ' '. number_to_words($r);
             }
         } else if($x < 1000000)
         {
             $w .= number_to_words(floor($x/1000)) .' thousand';
             $r = fmod($x, 1000);
             if($r > 0)
             {
                 $w .= ' ';
                 if($r < 100)
                 {
                     $w .= ' ';
                 }
                 $w .= number_to_words($r);
             }
         } else {
             $w .= number_to_words(floor($x/1000000)) .' million';
             $r = fmod($x, 1000000);
             if($r > 0)
             {
                 $w .= ' ';
                 if($r < 100)
                 {
                     $word .= ' ';
                 }
                 $w .= number_to_words($r);
             }
         }
     }
     return $w;
}

// demonstration

if(isset($_POST['num']))
{
    echo '<center>';
     echo '
     '.htmlspecialchars($_POST['num']).' = '.number_to_words($_POST['num']).'<p>
     <a href="'.$_SERVER['PHP_SELF'].'">Back to try</a></center>';
}else{
     echo '
     <table bgcolor=#8087bc align=center><tr bgcolor=#ffffff><td><form method="post" action="'.$_SERVER['PHP_SELF'].'">
         <input type="text" name="num"><td></tr>
         <tr bgcolor=#ffffff><td><input type="submit" value="number in words"><td></tr></table>
     </form>';
}

?>

No comments:

Post a Comment