Return to Snippet

Revision: 50051
at August 5, 2011 08:05 by lasavior


Initial Code
<?PHP

/*  Number to word converter
 *  (no decimal, 0-1E+100)
 *  
 *  Created: 8/4/2011 by lasavior
 *  Version: 1
 *  
 *  example: echo numbertoword('3210984286');
 *  output: 'three billion, two hundred and ten million, nine hundred eighty-four thousand, two hundred and eighty-six'
 *  
 */

function numbertoword($numbertoparse){

$number = (string)$numbertoparse;

$wordlist = 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',
	'tenfold' => array(
		0	=>	'zero',
		1	=>	'ten',
		2	=>	'twenty',
		3	=>	'thirty',
		4	=>	'fourty',
		5	=>	'fifty',
		6	=>	'sixty',
		7	=>	'seventy',
		8	=>	'eighty',
		9	=>	'ninety'),
	'thousandfoldnums' => array(
		'hundred',
		'thousand' => 3,
		'million' => 6,
		'billion' => 9,
		'trillion' => 12,
		'quadrillion' => 15,
		'quintillion' => 18,
		'sextillion' => 21,
		'septillion' => 24,
		'octillion' => 27,
		'nonillion' => 30,
		'decillion' => 33,
		'undecillion' => 36,
		'duodecillion' => 39,
		'tredecillion' => 42,
		'quattuordecillion' => 45,
		'quindecillion' => 48,
		'sexdecillion' => 51,
		'septendecillion' => 54,
		'octodecillion' => 57,
		'novemdecillion' => 60,
		'vigintillion' => 63,
		'unvigintillion' => 66,
		'duovigintillion' => 69,
		'tresvigintillion' => 72,
		'quattuortillion' => 75,
		'quinvigintillion' => 78,
		'sexvigintillion' => 81,
		'septentillion' => 84,
		'octovigintillion' => 87,
		'novemvigintillion' => 90,
		'trigintillion' => 93,
		'untrigintillion' => 96,
		'duotrigintillion' => 99,
		'googol' => 100));
$wordlist['thousandfoldtext'] = array_keys($wordlist['thousandfoldnums']);
		
if ($number > 999E+100 || !is_numeric($number) || !is_string($number)):
  
  die("Incompatible input. Must be a string & below 1,000 Googol");
  endif;




$number_length = strlen($number);

$number_length_div_3 = $number_length/3;




for ($count = ceil($number_length_div_3); $count > 0; $count--){

$number_length = strlen($number);

$number_length_div_3 = $number_length/3;

$segment_length_1 = is_int($number_length_div_3) ? $number_length_div_3 - 1 : floor($number_length_div_3);
$segment_length_2 = ($number_length - ($segment_length_1 * 3));

if ($number_length < 4):
  
  $segment_length_2 = $number_length;
  endif;

$segment = (int)substr($number, 0, $segment_length_2);

if ($segment == 0):
  
  //do nothing
elseif ($segment < 20):
  
  $number_word .= $wordlist[$segment] . " ";
elseif ($segment < 100):
  
  $two_digit_1 = (int)substr($segment, 0, 1);
  $two_digit_2 = (int)substr($segment, 1, 1);
  
  $number_word .= $wordlist['tenfold'][$two_digit_1];
  if ($two_digit_2 != 0):
    
    $number_word .= "-" . $wordlist[$two_digit_2];
    endif;
  $number_word .= " ";
else:
  
  $three_digit_1 = (int)substr($segment, 0, 1);
  $three_digit_2 = (int)substr($segment, 1, 1);
  $three_digit_2_1 = (int)substr($segment, 1, 2);
  $three_digit_3 = (int)substr($segment, 2, 1);
  
  $number_word .= $wordlist[$three_digit_1] . " hundred ";
  
  if ($three_digit_2 > 1 && $three_digit_3 == 0):
    
    $number_word .= "and ";
    endif;
  
  if ($three_digit_2_1 == 0):
    
    //do nothing
  elseif ($three_digit_2_1 < 20):
    
    $number_word .= "and " . $wordlist[$three_digit_2_1] . " ";
  else:
    
    if ($number_length < 4 && $three_digit_3 != 0):
      
      $number_word .= "and ";
      endif;
    $number_word .= $wordlist['tenfold'][$three_digit_2];
    if ($three_digit_3 != 0):
    
      $number_word .= "-" . $wordlist[$three_digit_3];
      endif;
    $number_word .= " ";
    endif;
  endif;

if ($number_length > 3 && $segment > 0):
  
  
  $number_word .= $wordlist['thousandfoldtext'][$segment_length_1] . ", ";
  endif;
  
$number = substr($number, $segment_length_2);
} //END FOR

return $number_word;
} //END FUNCTION
?>

Initial URL

                                

Initial Description
Takes a number (no decimal) and converts it to written words.Why'd i write it to be able to do such big numbers? Why not? 

Note: The numbers next to the 'thousandfoldnums' are for your reference (thats how many zeros/places there are in that number), they have no function in the script.

I wrote this as a simple exercise to see if i could. I was reading something and a user mentioned that this was difficult. Even at my slow and limited programming capabilities i still did this in under two hours (still longer than i anticipated, but given my speed its still relatively short), pretty simple really. So here it is for anyone that needs it.

Initial Title
Number to word converter (0-1 googol)

Initial Tags
number, convert

Initial Language
PHP