Categories

LinkedIn

Jonathan Anders

Share

Recent Comments

num2txt

This script will take an integer number and return the textual readout of that number.

< ?php
define("NC_SMALL", 
  serialize(array('Zero', 'One', 'Two', 
                  'Three', 'Four', 'Five', 
                  'Six', 'Seven', 'Eight', 
                  'Nine'))
);
define("NC_MEDIUM", 
  serialize(array('Zero', 'Ten', 'Twenty', 
                  'Thirty', 'Forty', 'Fifty', 
                  'Sixty', 'Seventy', 'Eighty', 
                  'Ninety'))
);
define("NC_LARGE", 
  serialize(array('Ten', 'Eleven', 'Twelve', 
                  'Thirteen', 'Fourteen', 'Fifteen', 
                  'Sixteen', 'Seventeen', 'Eighteen', 
                  'Nineteen'))
);
define("NC_HUGE", 
  serialize(array('Thousand', 'Million', 'Billion', 
                  'Trillion', 'Quadrillion', 'Quintillion', 
                  'Sextillion', 'Septillion', 'Octillion', 
                  'Nonillion', 'Decillion', 'Undecillion', 
                  'Duodecillion', 'Tredecillion', 'Quattuordecillion', 
                  'Quindecillion', 'Sexdecillion', 'Septendecillion', 
                  'Octodecillion', 'Novemdecillion', 'Vigintillion'))
);
function ConvertThree ($intArgNumber, $dispAnd = true)
{
  $intNumber = '';
  $strNumConv = '';
  $aySmall = unserialize(NC_SMALL);
  $ayMedium = unserialize(NC_MEDIUM);
  $ayLarge = unserialize(NC_LARGE);
  if ($intArgNumber > 99)
    $strNumConv .= $aySmall[$intArgNumber/100] . " Hundred";
  if (($dispAnd == true) and ($intArgNumber > 99))
    $strNumConv .= " and";
  $intNumber = $intArgNumber % 100;
  if ($intNumber > 19) {
    if ($intArgNumber > 99) 
      $strNumConv .= " ";
    $strNumConv .= $ayMedium[$intNumber/10];
    if ($intNumber % 10 > 0) 
      $strNumConv .= " " . $aySmall[$intNumber % 10];
  } elseif ($intNumber > 9) {
    if ($intArgNumber > 99)
      $strNumConv .= $ayLarge[$intNumber-10];
  } elseif ($intNumber > 0) {
    if ($intArgNumber > 99)
      $strNumConv .= " ";
    $strNumConv .= $aySmall[$intNumber];
  }
  return $strNumConv;
}
function ConvertAll ($intArgNumber)
{
  $ayHuge = unserialize(NC_HUGE);
  $strNumConv = '';
  if ($intArgNumber > 999)
  {
    if (strlen($intArgNumber) > 66)
      return "Entry too long.";
    for ($x = 0; $intArgNumber > 0; $x++)
    {
      if ($x > count($ayHuge) - 1)
      {
        $strNumConv = ConvertAll($intArgNumber, false) . " " . $ayHuge[$x-1] . " " . $strNumConv;
        $intArgNumber = 0;
      } else {
        $strGrabThree = substr($intArgNumber, -3);
        $intArgNumber = substr($intArgNumber, 0, -1 * strlen($strGrabThree));
        if ($strGrabThree > 0)
          $strNumConv = ConvertThree($strGrabThree, true) . " " . $ayHuge[$x-1] . " " . $strNumConv;
      }
    }
    return $strNumConv;
  } else {
    return ConvertThree ($intArgNumber);
  }
}
?>

Testing

As a test, I used the following snippet to generate 15 random numbers to show how it works.

while (strlen($b) < 15)
{
  srand ((double) microtime() * 1000000);
  $b .= rand(1,9);
  echo $b . " - " . ConvertAll($b) . "\r\n";
}

The result of the snippet is the following:

2 - Two
26 - Twenty Six
261 - Two Hundred and Sixty One
2619 - Two Thousand Six Hundred andNineteen
26195 - Twenty Six Thousand One Hundred and Ninety Five
261956 - Two Hundred and Sixty One Thousand Nine Hundred and Fifty Six
2619569 - Two Million Six Hundred andNineteen Thousand Five Hundred and Sixty Nine
26195695 - Twenty Six Million One Hundred and Ninety Five Thousand Six Hundred and Ninety Five
261956952 - Two Hundred and Sixty One Million Nine Hundred and Fifty Six Thousand Nine Hundred and Fifty Two
2619569528 - Two Billion Six Hundred andNineteen Million Five Hundred and Sixty Nine Thousand Five Hundred and Twenty Eight
26195695282 - Twenty Six Billion One Hundred and Ninety Five Million Six Hundred and Ninety Five Thousand Two Hundred and Eighty Two
261956952822 - Two Hundred and Sixty One Billion Nine Hundred and Fifty Six Million Nine Hundred and Fifty Two Thousand Eight Hundred and Twenty Two
2619569528229 - Two Trillion Six Hundred andNineteen Billion Five Hundred and Sixty Nine Million Five Hundred and Twenty Eight Thousand Two Hundred and Twenty Nine
26195695282292 - Twenty Six Trillion One Hundred and Ninety Five Billion Six Hundred and Ninety Five Million Two Hundred and Eighty Two Thousand Two Hundred and Ninety Two
261956952822923 - Two Hundred and Sixty One Trillion Nine Hundred and Fifty Six Billion Nine Hundred and Fifty Two Million Eight Hundred and Twenty Two Thousand Nine Hundred and Twenty Three

Popularity: 10% [?]

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> <pre lang="" line="" escaped="" highlight="">

Spam protection by WP Captcha-Free