Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. public function convert_number_to_words($number)
  2. {
  3.  
  4. $hyphen = '-';
  5. $conjunction = ' and ';
  6. $separator = ', ';
  7. $negative = 'negative ';
  8. $decimal = ' point ';
  9. $dictionary = array(
  10. 0 => 'zero',
  11. 1 => 'one',
  12. 2 => 'two',
  13. 3 => 'three',
  14. 4 => 'four',
  15. 5 => 'five',
  16. 6 => 'six',
  17. 7 => 'seven',
  18. 8 => 'eight',
  19. 9 => 'nine',
  20. 10 => 'ten',
  21. 11 => 'eleven',
  22. 12 => 'twelve',
  23. 13 => 'thirteen',
  24. 14 => 'fourteen',
  25. 15 => 'fifteen',
  26. 16 => 'sixteen',
  27. 17 => 'seventeen',
  28. 18 => 'eighteen',
  29. 19 => 'nineteen',
  30. 20 => 'twenty',
  31. 30 => 'thirty',
  32. 40 => 'fourty',
  33. 50 => 'fifty',
  34. 60 => 'sixty',
  35. 70 => 'seventy',
  36. 80 => 'eighty',
  37. 90 => 'ninety',
  38. 100 => 'hundred',
  39. 1000 => 'thousand',
  40. 1000000 => 'million',
  41. 1000000000 => 'billion',
  42. 1000000000000 => 'trillion',
  43. 1000000000000000 => 'quadrillion',
  44. 1000000000000000000 => 'quintillion'
  45. );
  46.  
  47. if (!is_numeric($number)) {
  48. return false;
  49. }
  50.  
  51. if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
  52. // overflow
  53. trigger_error(
  54. 'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
  55. E_USER_WARNING
  56. );
  57. return false;
  58. }
  59.  
  60. if ($number < 0) {
  61. return $negative . $this->convert_number_to_words(abs($number));
  62. }
  63.  
  64. $string = $fraction = null;
  65.  
  66. if (strpos($number, '.') !== false) {
  67. list($number, $fraction) = explode('.', $number);
  68. }
  69.  
  70. switch (true) {
  71. case $number < 21:
  72. $string = $dictionary[$number];
  73. break;
  74. case $number < 100:
  75. $tens = ((int) ($number / 10)) * 10;
  76. $units = $number % 10;
  77. $string = $dictionary[$tens];
  78. if ($units) {
  79. $string .= $hyphen . $dictionary[$units];
  80. }
  81. break;
  82. case $number < 1000:
  83. $hundreds = $number / 100;
  84. $remainder = $number % 100;
  85. $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
  86. if ($remainder) {
  87. $string .= $conjunction . $this->convert_number_to_words($remainder);
  88. }
  89. break;
  90. default:
  91. $baseUnit = pow(1000, floor(log($number, 1000)));
  92. $numBaseUnits = (int) ($number / $baseUnit);
  93. $remainder = $number % $baseUnit;
  94. $string = $this->convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
  95. if ($remainder) {
  96. $string .= $remainder < 100 ? $conjunction : $separator;
  97. $string .= $this->convert_number_to_words($remainder);
  98. }
  99. break;
  100. }
  101.  
  102. if (null !== $fraction && is_numeric($fraction)) {
  103. $string .= $decimal;
  104. $words = array();
  105. foreach (str_split((string) $fraction) as $number) {
  106. $words[] = $dictionary[$number];
  107. }
  108. $string .= implode(' ', $words);
  109. }
  110.  
  111. return $string;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement