Numeric to word currency converter (Indian Number System)




Few days back at my work there arouse a need of converting integer currency to its respective word form or sentence form. 

I developed the following  code in PHP to do the same. It works for Indian Currency and can manipulate values upto 999 crores.

Heres the code





public function numbertoword($number)
    {
        $denom= array('crore','lakh','thousand','hundred');
        $denom1 = array('one','two','three','four','five','six','seven','eight','nine');
        $denom10 = array('ten','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninty');
        $denom11 = array('eleven','twelve','thirteen','fouteen','fifteen','sixteen','seventeen','eighteen','nineteen');
        $i=1;$output='';$abc=0;
        if($number == 0)
        {
            return 'zero';
        }
        while(round($number) > 0)
        {
            $digit1 = fmod($number ,10);
            $number = floor($number / 10);
            $dummy = $number;
            if($i > 10)
            {
                return 'Number Exceeded Computation Limits';
            }
            if($i==1 || $i==4 || $i==6 || $i==8)
            {
                $digit2= fmod($dummy ,10);;
                if($digit2 == 1)
                {
                    if($digit1 != 0)
                    $number = floor($number / 10);
                }
            }
            if($digit1 == 0)
            {
                
                $i++;
                $test='';
                continue;
            }
            switch($i)
            {
                case 1:if (intval($digit2) == 1)
                        {
                            $output = $denom11[intval($digit1)-1].' '.$output;
                            $digit2 =0;
                            $i++;
                        }
                        else
                        {
                            $output = $denom1[intval($digit1)-1].' '.$output;
                        }
                        
                        break;
                case 2:$output = $denom10[intval($digit1)-1 ].' '.$output;
                        
                        break;
                
                case 3:$output = $denom1[intval($digit1)-1].' '.$denom[3].' '.$output;
                    break; 
                
                case 4:if (intval($digit2) == 1)
                        {        
                            $output = $denom11[intval($digit1)-1].' '.$denom[2].' '.$output;

                            $i++;
                        }
                        else if($digit2 == 0)
                        {
                            $output = $denom1[intval($digit1)-1].' '.$denom[2].' '.$output;
                        }
                        else
                            $test = $denom1[intval($digit1)-1];
                            
                        break;
                case 5:$output = $denom10[intval($digit1)-1].' '.$test.' '.$denom[2].' '.$output;
                        break;

                case 6:if (intval($digit2) == 1)
                        {        
                            $output = $denom11[intval($digit1)-1].' '.$denom[1].' '.$output;

                            $i++;
                        }
                        else if($digit2 == 0)
                        {
                            $output = $denom1[intval($digit1)-1].' '.$denom[1].' '.$output;
                        }
                        else
                            $test = $denom1[intval($digit1)-1];break;
                case 7:$output = $denom10[intval($digit1)-1].' '.$test.' '.$denom[1].' '.$output;break;

                case 8:if (intval($digit2) == 1)
                        {        
                            $output = $denom11[intval($digit1)-1].' '.$denom[0].' '.$output;

                            $i++;
                        }
                        else if($digit2 == 0)
                        {
                            $output = $denom1[intval($digit1)-1].' '.$denom[0].' '.$output;
                        }
                        else
                            $test = $denom1[intval($digit1)-1];break;
                case 9:$output = $denom10[intval($digit1)-1].' '.$test.' '.$denom[0].' '.$output;break;
                case 10:$output = $denom1[intval($digit1)-1].' '.$denom[3].' '.$output; break;
                
            }
            $i++;
        }
        return $output;
                
    }
This code is especially useful when you want to generate online bills and other documents like Challan etc. in your application where it becomes mandatory to mention the amount in words.

I have developed this code in about 2 hours and according to me it is 99% bug free but still if you find anything please feel free to let me know.

0 comments:

Post a Comment