瀏覽代碼

Fixes ZF-7712: Zend_XmlRpc_Value_Double rounds to 6 decimal digits

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@17921 44c647ce-9c0f-0410-b52a-842ac1e357ba
lars 16 年之前
父節點
當前提交
456e57ea05
共有 2 個文件被更改,包括 19 次插入3 次删除
  1. 3 3
      library/Zend/XmlRpc/Value/Double.php
  2. 16 0
      tests/Zend/XmlRpc/ValueTest.php

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

@@ -45,7 +45,9 @@ class Zend_XmlRpc_Value_Double extends Zend_XmlRpc_Value_Scalar
     public function __construct($value)
     public function __construct($value)
     {
     {
         $this->_type = self::XMLRPC_TYPE_DOUBLE;
         $this->_type = self::XMLRPC_TYPE_DOUBLE;
-        $this->_value = sprintf('%f',(float)$value);    // Make sure this value is float (double) and without the scientific notation
+        $precision = (int)ini_get('precision');
+        $formatString = '%1.' . $precision . 'f';
+        $this->_value = sprintf($formatString, (float)$value);
     }
     }
 
 
     /**
     /**
@@ -57,6 +59,4 @@ class Zend_XmlRpc_Value_Double extends Zend_XmlRpc_Value_Scalar
     {
     {
         return (float)$this->_value;
         return (float)$this->_value;
     }
     }
-
 }
 }
-

+ 16 - 0
tests/Zend/XmlRpc/ValueTest.php

@@ -189,6 +189,21 @@ class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
         $this->assertEquals($this->wrapXml($xml), $val->saveXML());
         $this->assertEquals($this->wrapXml($xml), $val->saveXML());
     }
     }
 
 
+    /**
+     * @group ZF-7712
+     */
+    public function testMarshallingDoubleWithHigherPrecisionFromNative()
+    {
+        if (ini_get('precision') < 7) {
+            $this->markTestSkipped('precision is too low');
+        }
+
+        $native = 0.1234567;
+        $value = Zend_XmlRpc_Value::getXmlRpcValue($native, Zend_XmlRpc_Value::XMLRPC_TYPE_DOUBLE);
+        $this->assertXmlRpcType('double', $value);
+        $this->assertSame($native, $value->getValue());
+    }
+
     // String
     // String
 
 
     public function testFactoryAutodetectsString()
     public function testFactoryAutodetectsString()
@@ -205,6 +220,7 @@ class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
                                     Zend_XmlRpc_Value::XMLRPC_TYPE_STRING);
                                     Zend_XmlRpc_Value::XMLRPC_TYPE_STRING);
 
 
         $this->assertXmlRpcType('string', $val);
         $this->assertXmlRpcType('string', $val);
+        vaR_dump($val);
         $this->assertSame($native, $val->getValue());
         $this->assertSame($native, $val->getValue());
     }
     }