Example #1
0
 /**
  * Checks that Text::number formats a number into english text
  *
  * @test
  * @dataProvider provider_number
  */
 public function test_number($expected, $number)
 {
     $this->assertSame($expected, Text::number($number));
 }
Example #2
0
 /**
  * Format a number to human-readable text.
  *
  *     // Display: one thousand and twenty-four
  *     echo Text::number(1024);
  *
  *     // Display: five million, six hundred and thirty-two
  *     echo Text::number(5000632);
  *
  * @param   integer $number number to format
  * @return  string
  * @since   3.0.8
  */
 public static function number($number)
 {
     // The number must always be an integer
     $number = (int) $number;
     // Uncompiled text version
     $text = array();
     // Last matched unit within the loop
     $last_unit = null;
     // The last matched item within the loop
     $last_item = '';
     foreach (Text::$units as $unit => $name) {
         if ($number / $unit >= 1) {
             // $value = the number of times the number is divisible by unit
             $number -= $unit * ($value = (int) floor($number / $unit));
             // Temporary var for textifying the current unit
             $item = '';
             if ($unit < 100) {
                 if ($last_unit < 100 && $last_unit >= 20) {
                     $last_item .= '-' . $name;
                 } else {
                     $item = $name;
                 }
             } else {
                 $item = Text::number($value) . ' ' . $name;
             }
             // In the situation that we need to make a composite number (i.e. twenty-three)
             // then we need to modify the previous entry
             if (empty($item)) {
                 array_pop($text);
                 $item = $last_item;
             }
             $last_item = $text[] = $item;
             $last_unit = $unit;
         }
     }
     if (count($text) > 1) {
         $and = array_pop($text);
     }
     $text = implode(', ', $text);
     if (isset($and)) {
         $text .= ' and ' . $and;
     }
     return $text;
 }