Advertisement
Guest User

Untitled

a guest
Oct 10th, 2013
1,656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. <?php
  2.  
  3. function convertNumber($number)
  4. {
  5. list($integer, $fraction) = explode(".", (string) $number);
  6.  
  7. $output = "";
  8.  
  9. if ($integer{0} == "-")
  10. {
  11. $output = "negative ";
  12. $integer = ltrim($integer, "-");
  13. }
  14. else if ($integer{0} == "+")
  15. {
  16. $output = "positive ";
  17. $integer = ltrim($integer, "+");
  18. }
  19.  
  20. if ($integer{0} == "0")
  21. {
  22. $output .= "zero";
  23. }
  24. else
  25. {
  26. $integer = str_pad($integer, 36, "0", STR_PAD_LEFT);
  27. $group = rtrim(chunk_split($integer, 3, " "), " ");
  28. $groups = explode(" ", $group);
  29.  
  30. $groups2 = array();
  31. foreach ($groups as $g)
  32. {
  33. $groups2[] = convertThreeDigit($g{0}, $g{1}, $g{2});
  34. }
  35.  
  36. for ($z = 0; $z < count($groups2); $z++)
  37. {
  38. if ($groups2[$z] != "")
  39. {
  40. $output .= $groups2[$z] . convertGroup(11 - $z) . (
  41. $z < 11
  42. && !array_search('', array_slice($groups2, $z + 1, -1))
  43. && $groups2[11] != ''
  44. && $groups[11]{0} == '0'
  45. ? " and "
  46. : ", "
  47. );
  48. }
  49. }
  50.  
  51. $output = rtrim($output, ", ");
  52. }
  53.  
  54. if ($fraction > 0)
  55. {
  56. $output .= " point";
  57. for ($i = 0; $i < strlen($fraction); $i++)
  58. {
  59. $output .= " " . convertDigit($fraction{$i});
  60. }
  61. }
  62.  
  63. return $output;
  64. }
  65.  
  66. function convertGroup($index)
  67. {
  68. switch ($index)
  69. {
  70. case 11:
  71. return " decillion";
  72. case 10:
  73. return " nonillion";
  74. case 9:
  75. return " octillion";
  76. case 8:
  77. return " septillion";
  78. case 7:
  79. return " sextillion";
  80. case 6:
  81. return " quintrillion";
  82. case 5:
  83. return " quadrillion";
  84. case 4:
  85. return " trillion";
  86. case 3:
  87. return " billion";
  88. case 2:
  89. return " million";
  90. case 1:
  91. return " thousand";
  92. case 0:
  93. return "";
  94. }
  95. }
  96.  
  97. function convertThreeDigit($digit1, $digit2, $digit3)
  98. {
  99. $buffer = "";
  100.  
  101. if ($digit1 == "0" && $digit2 == "0" && $digit3 == "0")
  102. {
  103. return "";
  104. }
  105.  
  106. if ($digit1 != "0")
  107. {
  108. $buffer .= convertDigit($digit1) . " hundred";
  109. if ($digit2 != "0" || $digit3 != "0")
  110. {
  111. $buffer .= " and ";
  112. }
  113. }
  114.  
  115. if ($digit2 != "0")
  116. {
  117. $buffer .= convertTwoDigit($digit2, $digit3);
  118. }
  119. else if ($digit3 != "0")
  120. {
  121. $buffer .= convertDigit($digit3);
  122. }
  123.  
  124. return $buffer;
  125. }
  126.  
  127. function convertTwoDigit($digit1, $digit2)
  128. {
  129. if ($digit2 == "0")
  130. {
  131. switch ($digit1)
  132. {
  133. case "1":
  134. return "ten";
  135. case "2":
  136. return "twenty";
  137. case "3":
  138. return "thirty";
  139. case "4":
  140. return "forty";
  141. case "5":
  142. return "fifty";
  143. case "6":
  144. return "sixty";
  145. case "7":
  146. return "seventy";
  147. case "8":
  148. return "eighty";
  149. case "9":
  150. return "ninety";
  151. }
  152. } else if ($digit1 == "1")
  153. {
  154. switch ($digit2)
  155. {
  156. case "1":
  157. return "eleven";
  158. case "2":
  159. return "twelve";
  160. case "3":
  161. return "thirteen";
  162. case "4":
  163. return "fourteen";
  164. case "5":
  165. return "fifteen";
  166. case "6":
  167. return "sixteen";
  168. case "7":
  169. return "seventeen";
  170. case "8":
  171. return "eighteen";
  172. case "9":
  173. return "nineteen";
  174. }
  175. } else
  176. {
  177. $temp = convertDigit($digit2);
  178. switch ($digit1)
  179. {
  180. case "2":
  181. return "twenty-$temp";
  182. case "3":
  183. return "thirty-$temp";
  184. case "4":
  185. return "forty-$temp";
  186. case "5":
  187. return "fifty-$temp";
  188. case "6":
  189. return "sixty-$temp";
  190. case "7":
  191. return "seventy-$temp";
  192. case "8":
  193. return "eighty-$temp";
  194. case "9":
  195. return "ninety-$temp";
  196. }
  197. }
  198. }
  199.  
  200. function convertDigit($digit)
  201. {
  202. switch ($digit)
  203. {
  204. case "0":
  205. return "zero";
  206. case "1":
  207. return "one";
  208. case "2":
  209. return "two";
  210. case "3":
  211. return "three";
  212. case "4":
  213. return "four";
  214. case "5":
  215. return "five";
  216. case "6":
  217. return "six";
  218. case "7":
  219. return "seven";
  220. case "8":
  221. return "eight";
  222. case "9":
  223. return "nine";
  224. }
  225. }
  226.  
  227. $num = 500254.89;
  228. $split = explode('.',$num);
  229. $whole = convertNumber($split[0].".0");
  230. $cents = convertNumber($split[1].".0");
  231.  
  232. echo $whole." and ".$cents." cents";
  233.  
  234. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement