Posted By

lasavior on 08/05/11

Statistics

Viewed 392 times
Favorited by 0 user(s)

Number to word converter (0-1 googol)

/ 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.

Copy this code and paste it in your HTML
1. <?PHP
2.
3. /* Number to word converter
4.  * (no decimal, 0-1E+100)
5.  *
6.  * Created: 8/4/2011 by lasavior
7.  * Version: 1
8.  *
9.  * example: echo numbertoword('3210984286');
10.  * output: 'three billion, two hundred and ten million, nine hundred eighty-four thousand, two hundred and eighty-six'
11.  *
12.  */
13.
14. function numbertoword(\$numbertoparse){
15.
16. \$number = (string)\$numbertoparse;
17.
18. \$wordlist = array(
19. 0 => 'zero',
20. 1 => 'one',
21. 2 => 'two',
22. 3 => 'three',
23. 4 => 'four',
24. 5 => 'five',
25. 6 => 'six',
26. 7 => 'seven',
27. 8 => 'eight',
28. 9 => 'nine',
29. 10 => 'ten',
30. 11 => 'eleven',
31. 12 => 'twelve',
32. 13 => 'thirteen',
33. 14 => 'fourteen',
34. 15 => 'fifteen',
35. 16 => 'sixteen',
36. 17 => 'seventeen',
37. 18 => 'eighteen',
38. 19 => 'nineteen',
39. 'tenfold' => array(
40. 0 => 'zero',
41. 1 => 'ten',
42. 2 => 'twenty',
43. 3 => 'thirty',
44. 4 => 'fourty',
45. 5 => 'fifty',
46. 6 => 'sixty',
47. 7 => 'seventy',
48. 8 => 'eighty',
49. 9 => 'ninety'),
50. 'thousandfoldnums' => array(
51. 'hundred',
52. 'thousand' => 3,
53. 'million' => 6,
54. 'billion' => 9,
55. 'trillion' => 12,
57. 'quintillion' => 18,
58. 'sextillion' => 21,
59. 'septillion' => 24,
60. 'octillion' => 27,
61. 'nonillion' => 30,
62. 'decillion' => 33,
63. 'undecillion' => 36,
64. 'duodecillion' => 39,
65. 'tredecillion' => 42,
66. 'quattuordecillion' => 45,
67. 'quindecillion' => 48,
68. 'sexdecillion' => 51,
69. 'septendecillion' => 54,
70. 'octodecillion' => 57,
71. 'novemdecillion' => 60,
72. 'vigintillion' => 63,
73. 'unvigintillion' => 66,
74. 'duovigintillion' => 69,
75. 'tresvigintillion' => 72,
76. 'quattuortillion' => 75,
77. 'quinvigintillion' => 78,
78. 'sexvigintillion' => 81,
79. 'septentillion' => 84,
80. 'octovigintillion' => 87,
81. 'novemvigintillion' => 90,
82. 'trigintillion' => 93,
83. 'untrigintillion' => 96,
84. 'duotrigintillion' => 99,
85. 'googol' => 100));
86. \$wordlist['thousandfoldtext'] = array_keys(\$wordlist['thousandfoldnums']);
87.
88. if (\$number > 999E+100 || !is_numeric(\$number) || !is_string(\$number)):
89.
90. die("Incompatible input. Must be a string & below 1,000 Googol");
91. endif;
92.
93.
94.
95.
96. \$number_length = strlen(\$number);
97.
98. \$number_length_div_3 = \$number_length/3;
99.
100.
101.
102.
103. for (\$count = ceil(\$number_length_div_3); \$count > 0; \$count--){
104.
105. \$number_length = strlen(\$number);
106.
107. \$number_length_div_3 = \$number_length/3;
108.
109. \$segment_length_1 = is_int(\$number_length_div_3) ? \$number_length_div_3 - 1 : floor(\$number_length_div_3);
110. \$segment_length_2 = (\$number_length - (\$segment_length_1 * 3));
111.
112. if (\$number_length < 4):
113.
114. \$segment_length_2 = \$number_length;
115. endif;
116.
117. \$segment = (int)substr(\$number, 0, \$segment_length_2);
118.
119. if (\$segment == 0):
120.
121. //do nothing
122. elseif (\$segment < 20):
123.
124. \$number_word .= \$wordlist[\$segment] . " ";
125. elseif (\$segment < 100):
126.
127. \$two_digit_1 = (int)substr(\$segment, 0, 1);
128. \$two_digit_2 = (int)substr(\$segment, 1, 1);
129.
130. \$number_word .= \$wordlist['tenfold'][\$two_digit_1];
131. if (\$two_digit_2 != 0):
132.
133. \$number_word .= "-" . \$wordlist[\$two_digit_2];
134. endif;
135. \$number_word .= " ";
136. else:
137.
138. \$three_digit_1 = (int)substr(\$segment, 0, 1);
139. \$three_digit_2 = (int)substr(\$segment, 1, 1);
140. \$three_digit_2_1 = (int)substr(\$segment, 1, 2);
141. \$three_digit_3 = (int)substr(\$segment, 2, 1);
142.
143. \$number_word .= \$wordlist[\$three_digit_1] . " hundred ";
144.
145. if (\$three_digit_2 > 1 && \$three_digit_3 == 0):
146.
147. \$number_word .= "and ";
148. endif;
149.
150. if (\$three_digit_2_1 == 0):
151.
152. //do nothing
153. elseif (\$three_digit_2_1 < 20):
154.
155. \$number_word .= "and " . \$wordlist[\$three_digit_2_1] . " ";
156. else:
157.
158. if (\$number_length < 4 && \$three_digit_3 != 0):
159.
160. \$number_word .= "and ";
161. endif;
162. \$number_word .= \$wordlist['tenfold'][\$three_digit_2];
163. if (\$three_digit_3 != 0):
164.
165. \$number_word .= "-" . \$wordlist[\$three_digit_3];
166. endif;
167. \$number_word .= " ";
168. endif;
169. endif;
170.
171. if (\$number_length > 3 && \$segment > 0):
172.
173.
174. \$number_word .= \$wordlist['thousandfoldtext'][\$segment_length_1] . ", ";
175. endif;
176.
177. \$number = substr(\$number, \$segment_length_2);
178. } //END FOR
179.
180. return \$number_word;
181. } //END FUNCTION
182. ?> Subscribe to comments