feed me feed me chillireward

Article (in detail)

Number to text function, to render the number in words

 

Today Im sharing an interesting function that you can add to your wordpress blog, to render total comments in words.
All you have to do is to copy the funtion below into your functions.php file.

$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" );

function int_to_words($x)

{

     global $nwords;

     if(!is_numeric($x))

     {

         $w = '#';

     }else if(fmod($x, 1) != 0)

     {

         $w = '#';

     }else{

         if($x < 0)

         {

             $w = 'minus ';

             $x = -$x;

         }else{

             $w = '';

         }

         if($x < 21)

         {

             $w .= $nwords[$x];

         }else if($x < 100)

         {

             $w .= $nwords[10 * floor($x/10)];

             $r = fmod($x, 10);

             if($r > 0)

             {

                 $w .= ‘-’. $nwords[$r];

             }

         } else if($x < 1000)

         {

             $w .= $nwords[floor($x/100)] .' hundred';

             $r = fmod($x, 100);

             if($r > 0)

             {

                 $w .= ‘ and ‘. int_to_words($r);

             }

         } else if($x < 1000000)

         {

             $w .= int_to_words(floor($x/1000)) .' thousand';

             $r = fmod($x, 1000);

             if($r > 0)

             {

                 $w .= ‘ ‘;

                 if($r < 100)

                 {

                     $w .= 'and ';

                 }

                 $w .= int_to_words($r);

             }

         } else {

             $w .= int_to_words(floor($x/1000000)) .' million';

             $r = fmod($x, 1000000);

             if($r > 0)

             {

                 $w .= ‘ ‘;

                 if($r < 100)

                 {

                     $word .= 'and ';

                 }

                 $w .= int_to_words($r);

             }

         }

     }

     return $w;

} 		

You need to invoke the function wherever you want to express the number in words. For instance, I have invoked the function in my comments.php file, like so..

$comment_count=get_comments_number();
$plural="";
//this part generates the word 'comments' for two comments and above, otherwise it generates the word 'comment'
if ($comment_count>1){$plural="s";}
echo int_to_words($comment_count).'comment'.$plural;

Popularity: 6% [?]

Page copy
protected against web site content infringement by Copyscape

Liked this article? Subscribe to the article feed via feedburner. Dont want to be hassled by feeds, you can Subscribe by E-mail. Want to know who said what? Get my comments feed. Dont know what a RSS feed is? Read about RSS feeds.

Other Posts for you to Enjoy

Feedback Please!

Note: Your comments may take time to appear, as they are moderated for the first time..

All comments are followed.

 
 
Security Image