ID: 24591
User updated by: arnarb at oddi dot is
Reported By: arnarb at oddi dot is
Status: Bogus
Bug Type: Scripting Engine problem
Operating System: Linux 2.4.18
PHP Version: 5.0.0b2-dev, 4.3.3RC2-dev
New Comment:

This causes some awkward problems. The mssql module I'm using takes
doubles and floats from the database and converts them to strings with
printf, which means I'm not able to do any calculations with them
unless replacing the , with . first.

Maybe this is a bug with the db module?


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

[2003-07-20 21:42:43] [email]iliaaphp.net[/email]

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the doentation available at
[url]http://www.php.net/manual/[/url] and the instructions on how to report
a bug at [url]http://bugs.php.net/how-to-report.php[/url]

This is expected behaviour, consider what you are doing. You generate a
sting containing "3,233" and then add 1 to it. PHP can only handle
floats that use "." as a decimal point hence when the string is
converted to a number it stops at the "," and 'rounds' the number to 3.
Hence the result of 4 when 1 is added. To avoid this problem you
should've performed the addition before printing the output.

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

[2003-07-10 11:26:58] [email]sniperphp.net[/email]

<?php

printf("%.3f\n", 3.233);
var_dump(sprintf("%.3f", 3.233) + 1);

setlocale(LC_ALL, "is_IS");

printf("%.3f\n", 3.233);
var_dump(sprintf("%.3f", 3.233) + 1);

?>

Output:

3.233
float(4.233)
3,233
int(4)



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

[2003-07-10 10:52:19] arnarb at oddi dot is

Description:
------------
When strings containing numbers in the locale format, and the locale
uses , as the decimal seperator, converting the string to a float cuts
off at the , and returns the integer part.

This was addressed in bugs #17105, #17815 and others. Those reports
were closed and the problem was claimed to be fixed in CVS as of
November 2002 by iliaa and sniper.

This bug is however still present in 4.3.2, as the reproduce code
demonstrates.

A quick look indicated that libc's strtod was being used for the
conversion, I verified that it is working on my platform.

Reproduce code:
---------------
<?php
printf("%.3f\n", 3.233);
print sprintf("%.3f", 3.233)+1;
print "\n";

setlocale(LC_ALL, "is_IS");

printf("%.3f\n", 3.233);
print sprintf("%.3f", 3.233)+1;
print "\n";
?>

Expected result:
----------------
3.233
4.233
3,233
4,233

Actual result:
--------------
3.233
4.233
3,233
4


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


--
Edit this bug report at [url]http://bugs.php.net/?id=24591&edit=1[/url]