Re: [PHP-DEV] Locale-independent double-to-string cast

2013-10-03 Thread Marc Bennewitz

Am 02.10.2013 20:38, schrieb Adam Harvey:
> On 2 October 2013 10:57, Christopher Jones  
> wrote:
>> On 10/02/2013 10:26 AM, Nikita Popov wrote:
>>> 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.
>>
>> I'd like to see float/double casts recognize the locale's decimal
>> separator.
> 
> That's an interesting idea, and arguably one that's more in line with
> what PHP has been doing.
> 
> I'd be really interested to hear from people in countries where the
> decimal separator is a comma, since I don't have any experience with
> this myself as an Anglophone — do you run PHP in your native locale,
> and if so, would it be better to always have dots, as Nikita suggests,
> or support parsing numbers with commas? (Or some combination therein.)

+1

This is an issue I often ran into.
In my opinion on type casting a value from/to string it should use the
standard computer format and not a localized one. To format to a
localized format we have a function named "number_format" and since PHP
5.3 the class "NumberFormatter".

Additionally "setlocale" is a process operation that makes issues on
multi threaded envs. So temporary reset the locale isn't same, too.

My little two cent from germany

Marc

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] Locale-independent double-to-string cast

2013-10-02 Thread Nikita Popov
On Wed, Oct 2, 2013 at 7:57 PM, Christopher Jones <
christopher.jo...@oracle.com> wrote:

> 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:
>

That will work fine for the specific case of doing a (float) cast, but it
will not solve the problem in general. Oracle specifically may not have a
problem with ","-numbers, but practically everything else does :/

Nikita


Re: [PHP-DEV] Locale-independent double-to-string cast

2013-10-02 Thread Adam Harvey
On 2 October 2013 10:57, Christopher Jones  wrote:
> On 10/02/2013 10:26 AM, Nikita Popov wrote:
>> 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.
>
> I'd like to see float/double casts recognize the locale's decimal
> separator.

That's an interesting idea, and arguably one that's more in line with
what PHP has been doing.

I'd be really interested to hear from people in countries where the
decimal separator is a comma, since I don't have any experience with
this myself as an Anglophone — do you run PHP in your native locale,
and if so, would it be better to always have dots, as Nikita suggests,
or support parsing numbers with commas? (Or some combination therein.)

Adam

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] Locale-independent double-to-string cast

2013-10-02 Thread Christopher Jones



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:

  

The output is:

string(7) "123,567"
float(123)   // Ideally this would be 123,567

Chris

--
christopher.jo...@oracle.com  http://twitter.com/ghrd
Free PHP & Oracle book:
http://www.oracle.com/technetwork/topics/php/underground-php-oracle-manual-098250.html

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP-DEV] Locale-independent double-to-string cast

2013-10-02 Thread Nikita Popov
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, first off, this is how PHP currently behaves:

https://bugs.php.net/bug.php?id=55160).

 3. Another case where things can seriously go wrong is outputting doubles
in the generation of code (be it PHP for caching purposes or JS for the
client). To get around the issue you usually need to introduce some very
ugly code that changes the LC_NUMERIC locale to 'C'. E.g. this is what Twig
uses in its code generator:

if (false !== $locale = setlocale(LC_NUMERIC, 0)) {
setlocale(LC_NUMERIC, 'C');
}

$this->raw($value);

if (false !== $locale) {
setlocale(LC_NUMERIC, $locale);
}

In this case (just like with MySQL) you will also not just emit wrong
code, but it can end up being working code with totally different semantics
(as "," is usually a function argument separator).

These are just three random examples I came up with, but I've seen this
issue a lot of times. The insidious thing about it is that, with very high
probability, you will not notice this issue during development (because you
don't use locales), it will only turn up later.

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 think not having the locale-dependent output won't be much of a loss for
anyone, because if you need to actually localize the output of your
numbers, it is very likely that just replacing the decimal separator is not
enough (you will at least want to have a thousands-separator as well, i.e.
you want to use number_format).

So, thoughts?

Nikita
(Sorry for the long rant)