Procházet zdrojové kódy

ZF-7712: Trimming trailing zeros when encoding floats

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19562 44c647ce-9c0f-0410-b52a-842ac1e357ba
lars před 16 roky
rodič
revize
7d723bbcf9

+ 1 - 1
library/Zend/XmlRpc/Value/Double.php

@@ -47,7 +47,7 @@ class Zend_XmlRpc_Value_Double extends Zend_XmlRpc_Value_Scalar
         $this->_type = self::XMLRPC_TYPE_DOUBLE;
         $precision = (int)ini_get('precision');
         $formatString = '%1.' . $precision . 'f';
-        $this->_value = sprintf($formatString, (float)$value);
+        $this->_value = rtrim(sprintf($formatString, (float)$value), '0');
     }
 
     /**

+ 21 - 1
tests/Zend/XmlRpc/ValueTest.php

@@ -248,9 +248,11 @@ class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
 
     /**
      * @group ZF-7712
+     * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
      */
-    public function testMarshallingDoubleWithHigherPrecisionFromNative()
+    public function testMarshallingDoubleWithHigherPrecisionFromNative(Zend_XmlRpc_Generator_Abstract $generator)
     {
+        Zend_XmlRpc_Value::setGenerator($generator);
         if (ini_get('precision') < 7) {
             $this->markTestSkipped('precision is too low');
         }
@@ -259,6 +261,24 @@ class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
         $value = Zend_XmlRpc_Value::getXmlRpcValue($native, Zend_XmlRpc_Value::XMLRPC_TYPE_DOUBLE);
         $this->assertXmlRpcType('double', $value);
         $this->assertSame($native, $value->getValue());
+        $this->assertSame('<value><double>0.1234567</double></value>', trim($value->saveXml()));
+    }
+
+    /**
+     * @group ZF-7712
+     * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
+     */
+    public function testMarshallingDoubleWithHigherPrecisionFromNativeWithTrailingZeros(Zend_XmlRpc_Generator_Abstract $generator)
+    {
+        Zend_XmlRpc_Value::setGenerator($generator);
+        if (ini_get('precision') < 7) {
+            $this->markTestSkipped('precision is too low');
+        }
+        $native = 0.1;
+        $value = Zend_XmlRpc_Value::getXmlRpcValue($native, Zend_XmlRpc_Value::XMLRPC_TYPE_DOUBLE);
+        $this->assertXmlRpcType('double', $value);
+        $this->assertSame($native, $value->getValue());
+        $this->assertSame('<value><double>0.1</double></value>', trim($value->saveXml()));
     }
 
     // String