Indian currency number 2 word conversion

I had created number to word conversion function earlier. Now while working upon a web based financial management application I need to use this function once more. However I found my old function is not working properly with big numbers and also with currency amount(2 digit decimal value). So I need to change the code a little bit. Ranajit suggested me to put this a post as he didn’t find out any such good conversion code. He informed me about some PEAR class [Numbers_Words], but it provides USD as currency.

First check this working example here – CurrencyToWords, then the code is here:-

  1. <?php
  2. class CurrencyToWords
  3. {
  4.   function translateInWords($amount)
  5.   {
  6.      $CurnStr = ;
  7.      $amtArr = explode(‘.’, $amount);
  8.      $rupee = $this->numbers2words($amtArr[0]);
  9.      $paisa = $this->numbers2words($amtArr[1]);
  10.      $CurnStr .= ‘Rupees ‘.$rupee;
  11.      $CurnStr .= (trim($paisa) != ‘Zero’ && trim($paisa) != )?‘ & Paisa ‘.$paisa.‘ Only’:‘ Only’;
  12.      return $CurnStr;
  13.   }
  14.  
  15.   function numbers2words($number2convert)
  16.   {
  17.      $CodeArray = array(
  18.                        ‘upto10′ => array("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"),
  19.                        ‘upto20′ => array("Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty"),
  20.                        ‘series10′ => array("", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"),
  21.                        ‘suffixwords’ =>array("Thousand", "Lakh", "Crore"),
  22.                        );
  23.      $divisor = 100;
  24.      $Counter = 0;
  25.      $Rslt = "";
  26.      $cNum = (float)$number2convert;
  27.      $rNum = floor($cNum%1000);
  28.      $cNum = floor($cNum/1000);
  29.      $Rslt .= $this->lowerThousandInWords($rNum, $CodeArray);
  30.      while($cNum > 0)
  31.      {
  32.         if($Counter == (count($CodeArray[‘suffixwords’]) 1))
  33.         {
  34.            $Rslt = $this->numbers2words($cNum).‘ ‘.$CodeArray[‘suffixwords’][$Counter].‘ ‘.$Rslt;
  35.            break;
  36.         }
  37.         $rNum = floor($cNum%$divisor);
  38.         $cNum = floor($cNum/$divisor);
  39.         if($rNum !=0)
  40.         {
  41.            $Rslt = $this->lowerHundredInWords($rNum, $CodeArray).‘ ‘.$CodeArray[‘suffixwords’][$Counter].‘ ‘.$Rslt;
  42.         }
  43.         $Counter++;
  44.      }
  45.      return $Rslt;
  46.   }
  47.  
  48.   function lowerHundredInWords($num, $codeArray)
  49.   {
  50.      $rStr = "";
  51.      if($num >= 100)
  52.      {
  53.         return FALSE; // number is higher than 100
  54.      }
  55.      if($num <= 10)
  56.      {
  57.         return $codeArray[‘upto10′][$num];
  58.      }
  59.      if($num <= 20)
  60.      {
  61.         return $codeArray[‘upto20′][$num 10 1];
  62.      }
  63.      $lsd = floor($num%10);
  64.      $num /= 10;
  65.      $msd = floor($num%10);
  66.      $rStr .= ($msd > 0)?$codeArray[‘series10′][$msd]:;
  67.      $rStr .= ‘ ‘;
  68.      $rStr .= ($lsd > 0)?$codeArray[‘upto10′][$lsd]:;
  69.      return $rStr;
  70.   }
  71.  
  72.   function lowerThousandInWords($num, $codeArray)
  73.   {
  74.      if ($num >= 1000)
  75.      {
  76.         return FALSE; //number is higher than 1000
  77.      }
  78.      $rStr = $this->lowerHundredInWords(floor($num%100), $codeArray);
  79.      $num = floor($num/100);
  80.      $hund = floor($num%10);
  81.      $rStr = ($hund > 0 ? ($codeArray[‘upto10′][$hund].‘ Hundred ‘) : ).$rStr;
  82.      return $rStr;
  83.   }
  84. }
  85. ?>

You can use it like

  1. <?php
  2. $c2w = new CurrencyToWords();
  3. $amt = 462572.45;
  4. echo $c2w->translateInWords($amt);
  5. // Shows : Rupees Four Lakh Sixty Two Thousand Five Hundred Seventy Two & Paisa Forty Five Only
  6. ?>

Hope this helps :)

Leave a Reply