Esempio n. 1
0
 /**
  * Set a new type, and convert the value
  *
  * @param  string $type New type to set
  * @throws Zend\Measure\Exception
  * @return Zend\Measure\AbstractMeasure
  */
 public function setType($type)
 {
     if (empty($this->_units[$type])) {
         throw new Exception("Type ({$type}) is unknown");
     }
     if (empty($this->_type)) {
         $this->_type = $type;
     } else {
         // Convert to standard value
         $value = $this->_value;
         if (is_array($this->_units[$this->getType()][0])) {
             foreach ($this->_units[$this->getType()][0] as $key => $found) {
                 switch ($key) {
                     case "/":
                         if ($found != 0) {
                             $value = call_user_func(Math\Math::$div, $value, $found, 25);
                         }
                         break;
                     case "+":
                         $value = call_user_func(Math\Math::$add, $value, $found, 25);
                         break;
                     case "-":
                         $value = call_user_func(Math\Math::$sub, $value, $found, 25);
                         break;
                     default:
                         $value = call_user_func(Math\Math::$mul, $value, $found, 25);
                         break;
                 }
             }
         } else {
             $value = call_user_func(Math\Math::$mul, $value, $this->_units[$this->getType()][0], 25);
         }
         // Convert to expected value
         if (is_array($this->_units[$type][0])) {
             foreach (array_reverse($this->_units[$type][0]) as $key => $found) {
                 switch ($key) {
                     case "/":
                         $value = call_user_func(Math\Math::$mul, $value, $found, 25);
                         break;
                     case "+":
                         $value = call_user_func(Math\Math::$sub, $value, $found, 25);
                         break;
                     case "-":
                         $value = call_user_func(Math\Math::$add, $value, $found, 25);
                         break;
                     default:
                         if ($found != 0) {
                             $value = call_user_func(Math\Math::$div, $value, $found, 25);
                         }
                         break;
                 }
             }
         } else {
             $value = call_user_func(Math\Math::$div, $value, $this->_units[$type][0], 25);
         }
         $slength = strlen($value);
         $length = 0;
         for ($i = 1; $i <= $slength; ++$i) {
             if ($value[$slength - $i] != '0') {
                 $length = 26 - $i;
                 break;
             }
         }
         $this->_value = Math\Math::round($value, $length);
         $this->_type = $type;
     }
     return $this;
 }
Esempio n. 2
0
 public function testNegativeRounding()
 {
     $this->assertEquals('-3', Math::round('-3.4'));
     $this->assertEquals(round(-3.4), Math::round('-3.4'));
     $this->assertEquals('-4', Math::round('-3.5'));
     $this->assertEquals(round(-3.5), Math::round('-3.5'));
     $this->assertEquals('-4', Math::round('-3.6'));
     $this->assertEquals(round(-3.6), Math::round('-3.6'));
     $this->assertEquals('-4', Math::round('-3.6', 0));
     $this->assertEquals(round(-3.6, 0), Math::round('-3.6', 0));
     $this->assertEquals('-1.96', Math::round('-1.95583', 2), '', 0.02);
     $this->assertEquals(round(-1.95583, 2), Math::round('-1.95583', 2), '', 0.02);
     $this->assertEquals(-1242000, Math::round('-1241757', -3), '', 250);
     $this->assertEquals(round(-1241757, -3), Math::round('-1241757', -3), '', 250);
     $this->assertEquals(-5.05, Math::round('-5.045', 2), '', 0.02);
     $this->assertEquals(round(-5.045, 2), Math::round('-5.045', 2), '', 0.02);
     $this->assertEquals(-5.06, Math::round('-5.055', 2), '', 0.02);
     $this->assertEquals(round(-5.055, 2), Math::round('-5.055', 2), '', 0.02);
 }