convert number to words in php



<?php

$one= 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                  => 'fourteen',
        15                  => 'fifteen',
        16                  => 'sixteen',
        17                  => 'seventeen',
        18                  => 'eighteen',
        19                  => 'nineteen'
        );
       
    $ten = array (
        0    =>    '',
        1    =>    '',
        2    =>    'twenty',
        3    =>    'thirty',
        4    =>    'fourty',
        5    =>    'fifty',
        6    =>    'sixty',
        7    =>    'seventy',
        8    =>    'eighty',
        9    =>    'ninety'
        );
   
function num2word($n)
{
    echo "Enter no between 1 to 999999999 \n";
    if($n<=0 && $n>999999999)
        echo "please enter valid no";
    else
 {
                  pw((($n/10000000)%100)," crore ");
                  pw((($n/100000)%100)," lakh ");
                  pw((($n/1000)%100)," thousand ");
                  pw((($n/100)%10)," hundred ");
                  pw(($n%100)," ");
 }   
}       

function pw($n,$ch)
{
 global $one;
 global $ten;

 echo ($n>19) ? ($ten[$n/10]." ".$one[$n%10]) : $one[$n];
 if($n) echo $ch;
 }

echo "\n". num2word(123456789);       
echo "\n". num2word(123477);
echo "\n". num2word(1000);
echo "\n". num2word(999);

?>       

Output :-

twelve crore thirty four lakhs fifty six thousand seven hundreds eighty nine

one lakhs twenty three thousand four hundreds seventy seven

one thousand

nine hundreds ninety nine

0 comments :