php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24591 Using locale with , as decimal sep., floats truncated on conversion from string
Submitted: 2003-07-10 10:52 UTC Modified: 2003-07-21 03:44 UTC
From: arnarb at oddi dot is Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.0b2-dev, 4.3.3RC2-dev OS: Linux 2.4.18
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: arnarb at oddi dot is
New email:
PHP Version: OS:

 

 [2003-07-10 10:52 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-07-10 11:26 UTC] sniper@php.net
<?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-20 21:42 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

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-21 03:44 UTC] arnarb at oddi dot is
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?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Sep 11 09:02:40 2024 UTC