Re: Locale-independent double-to-string cast
| From: | Christopher Jones | Date: | Wed, 02 Oct 2013 17:57:21 +0000 |
| Subject: | Re: Locale-independent double-to-string cast | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-69460@lists.php.net to get a copy of this message | ||
On 10/02/2013 10:26 AM, Nikita Popov wrote:
Hi internals! I'd like to change our double-to-string casting behavior to be locale-independent and would appreciate some opinions as to whether you consider this feasible.
So, my suggestion is to change the (string) cast to always use "." as the decimal separator, independent of locale. The patch for this is very simple, just need to change a few occurrences of "%.*G" to "%.*H".I'd like to see float/double casts recognize the locale's decimal separator. It's perfectly fine in Oracle DB for numbers to be inserted/fetched with "," (or any other character) as the decimal separator: <?php
$c = oci_connect('hr', 'welcome', 'localhost/XE');
$s = oci_parse($c, "alter session set nls_territory = germany");
oci_execute($s);
$s = oci_parse($c, "select 123.567 as num from dual");
oci_execute($s);
$r = oci_fetch_array($s, OCI_ASSOC);
$n1 = $r['NUM']; // value as fetched
var_dump($n1);
setlocale(LC_ALL, 'de_DE'); // this has no effect on casting to float
$n2 = (float)$n1; // now cast it to a number
var_dump($n2);
?>
The output is:
string(7) "123,567"
float(123) // Ideally this would be 123,567
Chris
--
christopher.jones@oracle.com http://twitter.com/ghrd
Free PHP & Oracle book:
http://www.oracle.com/technetwork/topics/php/underground-php-oracle-manual-098250.html