]> granicus.if.org Git - php/commitdiff
fix double-to-string conversion utils
authorAntony Dovgal <tony2001@php.net>
Thu, 19 Apr 2007 09:30:49 +0000 (09:30 +0000)
committerAntony Dovgal <tony2001@php.net>
Thu, 19 Apr 2007 09:30:49 +0000 (09:30 +0000)
Zend/tests/double_to_string.phpt [new file with mode: 0644]
Zend/zend_strtod.c

diff --git a/Zend/tests/double_to_string.phpt b/Zend/tests/double_to_string.phpt
new file mode 100644 (file)
index 0000000..e6c66e2
--- /dev/null
@@ -0,0 +1,48 @@
+--TEST--
+double to string conversion tests
+--FILE--
+<?php
+
+$doubles = array(
+       290000000000000000,
+       290000000000000,
+       29000000000000,
+       29000000000000.123123,
+       29000000000000.7123123,
+       29000.7123123,
+       239234242.7123123,
+       0.12345678901234567890,
+       10000000000000,
+       100000000000000,
+       1000000000000000001,
+       100000000000001,
+       10000000000,
+       999999999999999,
+       9999999999999999,
+       (float)0
+       );
+
+foreach ($doubles as $d) {
+       var_dump((string)$d);
+}
+
+echo "Done\n";
+?>
+--EXPECTF--    
+string(7) "2.9E+17"
+string(7) "2.9E+14"
+string(14) "29000000000000"
+string(14) "29000000000000"
+string(14) "29000000000001"
+string(13) "29000.7123123"
+string(15) "239234242.71231"
+string(16) "0.12345678901235"
+string(14) "10000000000000"
+string(7) "1.0E+14"
+string(7) "1.0E+18"
+string(7) "1.0E+14"
+string(11) "10000000000"
+string(7) "1.0E+15"
+string(7) "1.0E+16"
+string(1) "0"
+Done
index 798d94a384e901763118aa0ccca8efb2f43d1d16..94d1383aa2dfe9fc4417c97878076d57f47d0359 100644 (file)
@@ -1707,7 +1707,14 @@ ZEND_API char * zend_dtoa(double _d, int mode, int ndigits, int *decpt, int *sig
                                        if (value(d) > 0.5 + value(eps))
                                                goto bump_up;
                                        else if (value(d) < 0.5 - value(eps)) {
-                                               while(*--s == '0');
+                                               /* cut ALL traling zeros only if the number of chars is greater than precision 
+                                                * otherwise cut only extra zeros
+                                                */
+                                               if (k < ndigits) {
+                                                       while(*--s == '0' && (s - s0) > k);
+                                               } else {
+                                                       while(*--s == '0');
+                                               }
                                                s++;
                                                goto ret1;
                                        }