Re: Inconsistent float to string vs. string to floatcasting
| From: | Lauri Kenttä | Date: | Wed, 02 Jan 2019 19:31:24 +0000 |
| Subject: | Re: Inconsistent float to string vs. string to floatcasting | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-103671@lists.php.net to get a copy of this message | ||
On 2019-01-02 19:22, Zeev Suraski wrote:
1. I'm not sure what you mean "not many people use this"? People don't convert floats to strings?People don't format their floats using setlocale + echo. People use things like sprintf and number_format to get the right number of decimals, and many use number_format or even str_replace to change the decimal separator because setlocale has weird side effects (like the one being discussed). On 2019-01-02 19:22, Zeev Suraski wrote:
3. I view a UX change as a big deal. As we should in a language that is very commonly used to create UI.Then what about existing UI bugs? Thanks to this discussion, I found exactly one instance of setlocale in my whole PHP code base (used to format a printf nicely), and I also found a bug where "stringparam=$x" was ill-formatted because of this and produced a visible error in a generated image (although not critical and thus unnoticed until now). I was certainly not relying on this behaviour. It was just bad luck that the output (at least when I saw it – don't know about other users!) was ”good enough” that I didn't notice the bug. On 2019-01-02 19:22, Zeev Suraski wrote:
4. This could effect not only UX, but also integration code. You could have PHP output feeding into something else - and suddenly, the format breaks. With the fix HAVING to be in the other side, no less.It's reasonable to expect that a float (with known range) is a valid number in most programming contexts such as CSS (width: <?= $float ?>px) or HTML (input type=number value=<?= $float ?>) or JavaScript (var width = <?= $float; ?>). Using number_format to fix these would feel almost as bad as using number_format before every arithmetic operation. Because of this behaviour, using setlocale will break many libraries which output floating-point values in any other context than user-visible text. On 2019-01-02 19:22, Zeev Suraski wrote:
With the fix HAVING to be in the other side, no less.How so? If you send floats, you can format them yourself (and you certainly should, if they are locale-dependent!). If you receive floats, you can parse them yourself. No need to change ”the other side”. -- Lauri Kenttä