php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49817 round() couldn't handle german floats with comma
Submitted: 2009-10-09 08:41 UTC Modified: 2009-10-09 09:14 UTC
From: wojtech at sam-germany dot de Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2SVN-2009-10-09 (snap) OS: Windows XP SP2
Private report: No CVE-ID: None
 [2009-10-09 08:41 UTC] wojtech at sam-germany dot de
Description:
------------
The problem is known, but the result can't be the last answer.
I must calculate the body mass index for my company.
Yesterday I registered rounding errors between 0.1 and 0.3.
The problem: german doctors declares weight as "63,3" and not as "63.3".
It was my fault trusting in PHPs typecasting.
BUT: why trying typecasting a stupid string, so round() gives a "correct" answer?
I except returning FALSE, if the given parameter contains something else than "0-9" and ".".

Reproduce code:
---------------
$var = "German doctors don't know the point.";
print round($var, 1);
$var = "3 german doctors dont't know the point.";
print round($var, 1);
$var = "3,467";
print round($var, 1);
$var = "3.465";
print round($var, 1);

Expected result:
----------------
FALSE
FALSE
FALSE
3,5

Actual result:
--------------
0
3
3
3.5

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-09 08:58 UTC] wojtech at sam-germany dot de
If you think, that I hadn't seen the note, you are wrong.
I can read:
"Note:  PHP doesn't handle strings like "12,300.2" correctly by default. See converting from strings."
(I still had said "the problem is known".)

But could you explain me the reason of the behaviour? 
Why don't change it to what everyone would expect?
Or I think so wrongly?
 [2009-10-09 09:00 UTC] pajoye@php.net
It expects a float, not a localized string representation of a number.
 [2009-10-09 09:14 UTC] wojtech at sam-germany dot de
Sure, but why PHP typecasting every given parameter?
Isn't it stupid returning 3, if the parameter is something like '3 doctors ...'?
OK - it's the developers task to make sure that parameters make sense, but results which seem correct could falsified everything.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 07:01:29 2024 UTC