wmde / Number

Numerical value objects, parsers and formatters
Other
18 stars 6 forks source link

Release 0.10.0 with refined float to string conversion #118

Closed thiemowmde closed 6 years ago

thiemowmde commented 6 years ago

I played around with the float-to-string conversion, tried a lot of different approaches I already tried before, and found a new one:

This patch fixes two of the listed issues, and (I believe) avoids all others:

Bug: T155910

mariushoch commented 6 years ago

Looking at the PHP sources (https://github.com/php/php-src/blob/414aaa207cc59c279d3fd5ed83f6b92890e57023/Zend/zend_operators.c#L560), I think you could also use sprintf("%.17G", $number); here (although it is to note that this is probably not using the same code path as serialize does, which uses gcvt internally).

Although sprintf can use a localized decimal point, which serialize can't (hard coded to .).

thiemowmde commented 6 years ago

I heavily updated the patch to use sprintf with %e now. That was new to me (and is not well documented, unfortunately). It indeed allows to format floats with a fixed number of significant digits, which is what we need here.