Convert Currency Number to words in Yii Framework

This class in very needed who is working in web application. This class convert currency number to word.

Example: 100 to one hundred. (Its really very beautiful)

1. Copy and Paste this class in you Controller Class

  1. public function int_to_words($x)
  2.    {       
  3.        $nwords = array(“zero”, “one”, “two”, “three”, “four”, “five”, “six”, “seven”, “eight”, “nine”, “ten”, “eleven”, “twelve”, “thirteen”, “fourteen”, “fifteen”, “sixteen”, “seventeen”, “eighteen”, “nineteen”, “twenty”, 30 => “thirty”, 40 => “forty”, 50 => “fifty”, 60 => “sixty”, 70 => “seventy”, 80 => “eighty”, 90 => “ninety” );
  4.        if(!is_numeric($x))
  5.        {
  6.            $w = ‘#’;
  7.        }else if(fmod($x, 1) != 0)
  8.        {
  9.            $w = ‘#’; 
  10.        }else{
  11.            if($x < 0)
  12.            {
  13.                $w = ‘minus ’;
  14.                $x = -$x;
  15.            }else{
  16.                $w = ;
  17.            } 
  18.            if($x < 21)
  19.            {
  20.                $w .= $nwords[$x];
  21.            }else if($x < 100)
  22.            {
  23.                $w .= $nwords[10 * floor($x/10)];
  24.                $r = fmod($x, 10); 
  25.                if($r > 0)
  26.                {
  27.                    $w .= ‘-’. $nwords[$r];
  28.                }
  29.            } else if($x < 1000)
  30.            {
  31.                $w .= $nwords[floor($x/100)] .‘ hundred’; 
  32.                $r = fmod($x, 100);
  33.                if($r > 0)
  34.                {
  35.                    $w .= ‘ and ’. $this->int_to_words($r);
  36.                }
  37.            } else if($x < 100000) 
  38.            {
  39.                $w .= $this->int_to_words(floor($x/1000)) .‘ thousand’;
  40.                $r = fmod($x, 1000);
  41.                if($r > 0)
  42.                {
  43.                    $w .= ‘ ’; 
  44.                    if($r < 100)
  45.                    {
  46.                        $w .= ‘and ’;
  47.                    }
  48.                    $w .= $this->int_to_words($r);
  49.                } 
  50.            } else {
  51.                $w .= $this->int_to_words(floor($x/100000)) .‘ lakh’;
  52.                $r = fmod($x, 100000);
  53.                if($r > 0)
  54.                {
  55.                    $w .= ‘ ’; 
  56.                    if($r < 100)
  57.                    {
  58.                        $word .= ‘and ’;
  59.                    }
  60.                    $w .= $this->int_to_words($r);
  61.                } 
  62.            }
  63.        }
  64.        return $w;
  65.    }

2. Now see carefully how to use int_to_words() function to get exact result. Suppose you want to use it in actionCreate() function. It is so simple to use.

  1. public function actionCreate()
  2. {
  3.    $total_sale_price_inword = $this->int_to_words(10000);
  4.    echo $total_sale_price_inword;
  5. }

now echo $total_sale_price_inword will be display ten thousand.

 

Enjoy It.

If any problem feel free to contact with me.

http://www.rajibkumar.com

me@rajibkumar.com

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>