ID:               35215
 User updated by:  timo dot hummel at 4fb dot de
 Reported By:      timo dot hummel at 4fb dot de
 Status:           Bogus
 Bug Type:         Strings related
 Operating System: any
 PHP Version:      5.0.5
 New Comment:

I know it is not a bug, it's rather a design issue. However, I suggest
to implement something to get the technical value of a float variable.
This design issue renders either floating-point values or locale
settings useless.

Something like http://www.php.net/strval which ignores the locale
settings would be more useful:

$float = 0.12;
$sql = "SELECT * FROM test WHERE value=".strval($float,
IGNORE_LOCALE);

It would be nice if something like that would be included in a later
version of the zend engine or in PHP itself.


Previous Comments:
------------------------------------------------------------------------

[2005-11-14 16:47:03] [EMAIL PROTECTED]

Not a bug.

I don't like that behaviour either, anyway there's number_format() and
*printf().


------------------------------------------------------------------------

[2005-11-14 15:51:28] timo dot hummel at 4fb dot de

Description:
------------
Currently, the whole way PHP does type juggling is affected using
setlocale. 

Right now, PHP respects the setlocale-Settings for LC_NUMERIC when
doing type-juggling (e.g. converting a float to a string or when
outputting data):

$float = 0.12;
setlocale(LC_ALL, "de_DE");
echo $float;

The simple example above outputs 0,12. If you need to build up SQL
queries, this behaviour is unwanted and causes errors:

$float = 0.12;
setlocale(LC_ALL, "de_DE");
$sql = "SELECT * FROM test WHERE value=$float;";

The query will be "juggled" to SELECT * FROM test WHERE value=0,12; -
which is not the expected result. 

If setlocale is used, there's no way (at least not to my knowledge) to
access the original, english (I also call it technical) representation
of a floating point number. 

This is not really a bug, but rather a limitation by design, but it
effectively prevents PHP developers from implementing multi-lingual
applications.



Reproduce code:
---------------
<?php
/* Nicer output for browsers */
echo "<pre>";

/* Predefine some variables to play around with */
$float = 0.12;
$sql = "SELECT * FROM test WHERE amount=";

setlocale(LC_ALL, "C");         // Set standard locale
echo strval($float). "\n";      // Outputs 0.12, which is OK
echo $sql . $float."\n";    // Outputs SELECT * FROM test WHERE
amount=0.12, which can be used by a database.


setlocale(LC_ALL, "de_DE");     // Set german locale
echo strval($float). "\n";      // Outputs 0,12, which is OK for display
purposes, but not for data processing
echo $sql . $float."\n";    // Outputs SELECT * FROM test WHERE
amount=0,12, which cannot be used by a database with english locale.

echo "</pre>";
?>



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=35215&edit=1

Reply via email to