Exemplo n.º 1
0
 /**
  * Convert number to word
  * http://www.karlrixon.co.uk/writing/convert-numbers-to-words-with-php/ (originally convert_number_to_words)
  *
  * Examples:
  * echo Numbers::toWord(123456789);
  * one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine
  *
  * echo Numbers::toWord(123456789.123);
  * one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine point one two three
  *
  * echo Numbers::toWord(-1922685.477);
  * negative one million, nine hundred and twenty-two thousand, six hundred and eighty-five point four seven seven
  *
  * float rounding can be avoided by passing the number as a string
  * echo Numbers::toWord(123456789123.12345); // rounds the fractional part
  * one hundred and twenty-three billion, four hundred and fifty-six million, seven hundred and eighty-nine thousand, one hundred and twenty-three point one two
  *
  * echo Numbers::toWord('123456789123.12345'); // does not round
  * one hundred and twenty-three billion, four hundred and fifty-six million, seven hundred and eighty-nine thousand, one hundred and twenty-three point one two three four five
  *
  * @param $number
  * @return bool|mixed|null|string
  * @throws \Exception
  */
 public static function toWord($number)
 {
     $hyphen = '-';
     $conjunction = ' and ';
     $separator = ', ';
     $negative = 'negative ';
     $decimal = ' point ';
     $dictionary = self::toWordDictionary();
     if (!is_numeric($number)) {
         return false;
     }
     if ($number >= 0 && (int) $number < 0 || (int) $number < 0 - PHP_INT_MAX) {
         throw new \Exception(__METHOD__ . ' only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX);
     }
     if ($number < 0) {
         return $negative . Numbers::toWord(abs($number));
     }
     $string = $fraction = null;
     if (strpos($number, '.') !== false) {
         list($number, $fraction) = explode('.', $number);
     }
     switch (true) {
         case $number < 21:
             $string = $dictionary[$number];
             break;
         case $number < 100:
             $tens = (int) ($number / 10) * 10;
             $units = $number % 10;
             $string = $dictionary[$tens];
             if ($units) {
                 $string .= $hyphen . $dictionary[$units];
             }
             break;
         case $number < 1000:
             $hundreds = $number / 100;
             $remainder = $number % 100;
             $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
             if ($remainder) {
                 $string .= $conjunction . Numbers::toWord($remainder);
             }
             break;
         default:
             $baseUnit = pow(1000, floor(log($number, 1000)));
             $numBaseUnits = (int) ($number / $baseUnit);
             $remainder = $number % $baseUnit;
             $string = Numbers::toWord($numBaseUnits) . ' ' . $dictionary[$baseUnit];
             if ($remainder) {
                 $string .= $remainder < 100 ? $conjunction : $separator;
                 $string .= Numbers::toWord($remainder);
             }
             break;
     }
     if (null !== $fraction && is_numeric($fraction)) {
         $string .= $decimal;
         $words = array();
         foreach (str_split((string) $fraction) as $number) {
             $words[] = $dictionary[$number];
         }
         $string .= implode(' ', $words);
     }
     return $string;
 }