예제 #1
0
 function test_number_with_delimiter()
 {
     $this->assertEqual("12,345,678", number_with_delimiter(12345678));
     $this->assertEqual("123,456", number_with_delimiter("123456"));
     $this->assertEqual("12,345,678.05", number_with_delimiter("12345678.05"));
     $this->assertEqual("12.345.678", number_with_delimiter(12345678, array('delimiter' => ".")));
     $this->assertEqual("12,345,678", number_with_delimiter(12345678, array('delimiter' => ",")));
     $this->assertEqual("12,345,678 05", number_with_delimiter(12345678.05, array('separator' => " ")));
     $this->assertEqual("112a", number_with_delimiter("112a"));
     $this->assertEqual("98 765 432,98", number_with_delimiter(98765432.98, array('delimiter' => " ", 'separator' => ",")));
 }
예제 #2
0
 /**
  * Rounds number to the given decimal precision
  *
  * @todo
  *   Loss of doc
  */
 public function number_with_precision($number, $options = array())
 {
     if (is_numeric($number)) {
         $number = (double) $number;
     } elseif (array_key_exists('raise', $options) && $options['raise'] == true) {
         throw new InvalidArgumentException('number_with_precision function only accepts numbers. Input was: ' . $number);
     }
     $options = array_merge(self::$DEFAULT_CURRENCY_VALUES, $options);
     $precision = $this->array_delete($options, 'precision');
     $significant = $this->array_delete($options, 'significant');
     $strip_insignificant_zeros = $this->array_delete($options, 'strip_insignificant_zeros');
     if ($significant && $precision > 0) {
         if ($number == 0) {
             $digits = 1;
             $rounded_number = 0;
         } else {
             $digits = floor(log10(abs($number)) + 1);
             $rounded_number = (double) round($number / (double) pow(10, $digits - $precision)) * pow(10, $digits - $precision);
             $digits = floor(log10(abs($rounded_number)) + 1);
         }
         $precision -= $digits;
         $precision = $precision > 0 ? $precision : 0;
         // don't let precision be negative
     } else {
         $rounded_number = (double) round($number, $precision);
     }
     $formatted_number = number_with_delimiter(sprintf("%01.{$precision}f", $rounded_number), $options);
     if ($strip_insignificant_zeros) {
         $escaped_separator = preg_quote($options['separator']);
         $formatted_number = preg_replace("/({$escaped_separator})(\\d*[1-9])?0+\\z/", '\\1\\2', $formatted_number);
         return preg_replace("/{$escaped_separator}\\z/", '', $formatted_number);
     } else {
         return $formatted_number;
     }
 }
 public function testNumberWithDelimiter()
 {
     $this->assertEqual('12,345,678', number_with_delimiter('12345678'));
     $this->assertEqual('12 345 678', number_with_delimiter('12345678', ' '));
 }