Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:103644 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 21029 invoked from network); 27 Dec 2018 21:26:09 -0000 Received: from unknown (HELO mout.gmx.net) (212.227.17.22) by pb1.pair.com with SMTP; 27 Dec 2018 21:26:09 -0000 Received: from [192.168.2.103] ([91.8.175.134]) by mail.gmx.com (mrgmx103 [212.227.17.168]) with ESMTPSA (Nemesis) id 0MY75A-1gprYM20hM-00Uvrp; Thu, 27 Dec 2018 18:57:18 +0100 To: Umberto Salsi , internals@lists.php.net References: Message-ID: <6740f450-28f0-e6a5-2e9f-bcbecec0f9c7@gmx.de> Date: Thu, 27 Dec 2018 18:57:20 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: de-DE Content-Transfer-Encoding: 8bit X-Provags-ID: V03:K1:PrEXpWZy6TVGq/N2PYoE2m7AKKq/T03otLC2qgFtIVDgMmjSRwB dROaXgWeNuLjhLowNYdD+RoYRJaue2pGrEUSrQQ6NAUOG0NdQoEG/agQLurjjc8vqK+BYon 63TPXQYSFd7IGvT8b7LWZj1gCUhDoMC5a2rrVG35RrnfVwutyKurYv1cTJtK0Ea8z30WLKf VvJSRmt+kcOLe9+wELlYw== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:TP8XgtRPxUA=:rzimb30SWYm7kGjY0UpgW7 IpX5CbhxdP6y7Tn0CESxbJ7B2yAMjuHYnW3k3CpDJj0EZSti7Nrau8uloczqCSLMxHrQcgN15 RMzRIahJSURtqiBDJxhC2zf+Gnq1NaVEE5tL5M23W674Q6e+zUGAJU4q9x0BAcL/Zx9/gVzRh 3/N/o+g/z9SZMoFr/gXZGjRbSe9j3lr5tC4v7xccIrcivUvEPZoGpY7kSIJSZylp2GbMVE1qy Klxi/bHtLWaVthjcmNcEismilvPhhpFBcG9f3dsWzpLID2mhEGhQxja0c9JjNeMElDqG7fGA+ yRxveq9E3fXkc/6AxQe/90CToax0za+k7vgoL5qw5fOy+mtJpgYIcAOuhmbGNepN6pS7T7+fI c2t7bJV9JnQ8nuPvk/qr46NnhIhK2xO9m83dnaopiwm/vPHgWeyrYSiRVkwxXE/bVt9W2jq78 qpxHGArb6VkeOqZfCwbw65wjtkmjNDhAtP1SiVsP1peu0o76ZLHzuHW8QDaa4R6N6N0orJ3y3 8E+ZSgB/nG2emowBeQ+SfpE3r34zme6em2fJw5SlzWSDxiyifgLKVXJ7kYE5l8dox14tR6+nK DeR89KG3e6OTdhnT39h2lKWjAVdYBatGOKbEnGCJtvAJl8CyPaw3H5kITyaaim8XwmVlpwW8g lEBDtOGe5wJzvEzRlJhV+twHNXV6pHlzeoHOOQzlCpiNX86ZwsVtwYP+DGIh+giaBxMfpRy8s 76WJvfNtBt6U4MuqreWH7hRBgHb/j8LLamBfBCe3LI3TYXw0urzTYLrm1tY15Qxm++KL6QUeZ +ECiJqZkWv3dsa4aYEfuJkDTFpKljmjPjd5hmHhyOzYsGV41qSiVrw8Da4xxj8dZ0oX7squy5 bvb+rUSbV9FzqLPbx3YWCK8D4yVcyWnO/LllNxT/M1ticMAqODHqTrM6YfLUJN Subject: Re: Inconsistent float to string vs. string to float casting From: cmbecker69@gmx.de ("Christoph M. Becker") On 27.12.2018 at 17:33, Umberto Salsi wrote: > cmbecker69@gmx.de ("Christoph M. Becker") wrote: > >> Regarding the decimal separator (aka. decimal point), the behavior of >> casting float to string is inconsistent with casting string to float. >> [...] > > I'm shocked... Lot of code here assumes (float) does the exact reverse of > (string); forunately most servers are configures with the default locale > "C", but I'm still concerned for libraries portability. > > Did you filed a bug for that? There is . > Anyway, here is some test code I wrote: > […] > Output: > FAILED with LC_NUMERIC=de, (string) 0.5: got 0,5, exp 0.5 > FAILED with LC_NUMERIC=de, sprintf("%f", 0.5): got 0,500000, exp 0.500000 > > The second failed test is expected, as printf, scanf and co. are intended > to be locale-aware. > > But the first one? Is there any simple, efficient and safe way to convert > back a string into float? Well, if you have the appropriate locale set, you can use scanf() with %f. If you don't want to rely on the per process locale, you could use NumberFormatter::parse() instead. filter_var() with FILTER_VALIDATE_FLOAT can also be used, and might be the best option if you don't know which decimal separator is used, and you are sure there are no thousands separators in the string. -- Christoph M. Becker