2
0
Просмотр исходного кода

[ZF-8009] Zend_Measure:

- added precision detection on type change

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18493 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas 16 лет назад
Родитель
Сommit
51185ec7f2
2 измененных файлов с 24 добавлено и 8 удалено
  1. 11 7
      library/Zend/Measure/Abstract.php
  2. 13 1
      tests/Zend/Measure/LengthTest.php

+ 11 - 7
library/Zend/Measure/Abstract.php

@@ -237,11 +237,6 @@ abstract class Zend_Measure_Abstract
 
             // Convert to standard value
             $value = $this->_value;
-            $prec  = 0;
-            if (strpos($this->_value, '.') !== false) {
-                $prec = strlen(substr($this->_value, strpos($this->_value, '.') + 1));
-            }
-
             if (is_array($this->_units[$this->getType()][0])) {
                 foreach ($this->_units[$this->getType()][0] as $key => $found) {
                     switch ($key) {
@@ -289,8 +284,17 @@ abstract class Zend_Measure_Abstract
                 $value = @call_user_func(Zend_Locale_Math::$div, $value, $this->_units[$type][0], 25);
             }
 
-            $this->_value = Zend_Locale_Math::round($value, $prec);
-            $this->_type = $type;
+            $slength = strlen($value);
+            $length  = 0;
+            for($i = 1; $i <= 25; ++$i) {
+                if ($value[$slength - $i] != '0') {
+                    $length = 26 - $i;
+                    break;
+                }
+            }
+
+            $this->_value = Zend_Locale_Math::round($value, $length);
+            $this->_type  = $type;
         }
         return $this;
     }

+ 13 - 1
tests/Zend/Measure/LengthTest.php

@@ -380,7 +380,6 @@ class Zend_Measure_LengthTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('-100 m', $value->__toString(), 'Value -100 m expected');
     }
 
-
     /**
      * test getConversionList
      * expected array
@@ -391,4 +390,17 @@ class Zend_Measure_LengthTest extends PHPUnit_Framework_TestCase
         $unit  = $value->getConversionList();
         $this->assertTrue(is_array($unit), 'Array expected');
     }
+
+    /**
+     * @ZF-8009
+     */
+    public function testConvertingToSmallerUnit()
+    {
+        $unit   = new Zend_Measure_Length(231, Zend_Measure_Length::CENTIMETER, 'de');
+        $unit2  = new Zend_Measure_Length(1, Zend_Measure_Length::METER, 'de');
+        $result = $unit->add($unit2);
+        $result->setType(Zend_Measure_Length::METER);
+
+        $this->assertEquals('3.31', $result->getValue());
+    }
 }