Function for generating Indian currency in words

gyaneshwar's picture


Here is a function for generating Indian currency in words using recursion. Below function is in PHP language. This function is very useful for generating invoice of billing. if anyone know same function in C or C++ please share with us .

##################### START CODE ##########################
// a function for converting Indian currency number to words   

function convert_digit_to_words($no)  //taking number as parameter
{   

//creating array  of word for each digit
 $words = array('0'=> 'Zero' ,'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','20' => 'twenty','30' => 'thirty','40' => 'forty','50' => 'fifty','60' => 'sixty','70' => 'seventy','80' => 'eighty','90' => 'ninty','100' => 'hundred','1000' => 'thousand','100000' => 'lakh','10000000' => 'crore');
 
 
 //for decimal number taking decimal part
 
$cash=(int)$no;  //take number wihout decimal
$decpart = $no - $cash; //get decimal part of number

$decpart=sprintf("%01.2f",$decpart); //take only two digit after decimal

$decpart1=substr($decpart,2,1); //take first digit after decimal
$decpart2=substr($decpart,3,1);   //take second digit after decimal  

$decimalstr='';

//if given no. is decimal than  preparing string for decimal digit's word

if($decpart>0)
{
 $decimalstr.="point ".$numbers[$decpart1]." ".$numbers[$decpart2];
}
 
    if($no == 0)
        return ' ';
    else {
    $novalue='';
    $highno=$no;
    $remainno=0;
    $value=100;
    $value1=1000;       
            while($no>=100)    {
                if(($value <= $no) &&($no  < $value1))    {
                $novalue=$words["$value"];
                $highno = (int)($no/$value);
                $remainno = $no % $value;
                break;
                }
                $value= $value1;
                $value1 = $value * 100;
            }       
          if(array_key_exists("$highno",$words))  //check if $high value is in $words array
              return $words["$highno"]." ".$novalue." ".convert_digit_to_words($remainno).$decimalstr;  //recursion
          else {
             $unit=$highno%10;
             $ten =(int)($highno/10)*10;            
             return $words["$ten"]." ".$words["$unit"]." ".$novalue." ".convert_digit_to_words($remainno
             ).$decimalstr; //recursion
           }
    }
}

##################### END CODE ##########################

Ex.
INPUT: 12345.34
OUTPUT:twelve thousand three hundred forty five point three four

rahulasati's picture
 #

The indian currency converter

The indian currency converter code in haskell is here:
http://www.studystuff.in/content/code-convert-indian-currency-words-hask...

 

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.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2 + 14 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

User login

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
14 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Navigation

Poll

Which Field you will join after graduation.
IT industry
43%
Govt. Sector
28%
Want to be start up
30%
Total votes: 40

Recent comments