/ Published in: PHP
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.
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?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; 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', 0 => 'zero', 1 => 'ten', 2 => 'twenty', 3 => 'thirty', 4 => 'fourty', 5 => 'fifty', 6 => 'sixty', 7 => 'seventy', 8 => 'eighty', 9 => 'ninety'), '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)); endif; $number_length_div_3 = $number_length/3; $number_length_div_3 = $number_length/3; $segment_length_2 = ($number_length - ($segment_length_1 * 3)); if ($number_length < 4): $segment_length_2 = $number_length; endif; if ($segment == 0): //do nothing elseif ($segment < 20): $number_word .= $wordlist[$segment] . " "; elseif ($segment < 100): $number_word .= $wordlist['tenfold'][$two_digit_1]; if ($two_digit_2 != 0): $number_word .= "-" . $wordlist[$two_digit_2]; endif; $number_word .= " "; else: $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; } //END FOR return $number_word; } //END FUNCTION ?>