php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45221 Number in string conversion with comma separator
Submitted: 2008-06-09 18:15 UTC Modified: 2008-06-10 13:33 UTC
From: walterquez at yahoo dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.2.6 OS: Windows 2003
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: walterquez at yahoo dot com
New email:
PHP Version: OS:

 

 [2008-06-09 18:15 UTC] walterquez at yahoo dot com
Description:
------------
When numbers in a string containing comma separators are converted to any numeric type, it strips any number to the right of the commas.

Reproduce code:
---------------
<?php
$a = '200';
$b = '1,000';

if ($a > $b) echo 'A is bigger'; else echo 'B is bigger';
?>

For some reason, because $b has a comma, it converts $b to 1, not 1000. If you remove the comma, it works fine.

To prove it, check the following.

$b = (int) $b; // same with (float), (double) or (real)
echo $b; // it prints 1, not 1000.

I even included, setlocale(LC_ALL, 'en'); but no luck.

Expected result:
----------------
B is bigger

Actual result:
--------------
A is bigger

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-06-10 13:11 UTC] felipe@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

That is expected, the float value doesn't uses comma.
http://docs.php.net/float
 [2008-06-10 13:33 UTC] walterquez at yahoo dot com
This is not specific to a float, but also an integer.

Floats, or any numeric type should use commas, otherwise what is the purpose in using the thousands_sep parameter in localeconv if it is not used?

In a HTML form, if someone enters 1,000 (which is normal) rather than 1000, PHP will treat it as a 1, because of the comma. This is because HTML forms passes fields as strings, not numbers.

For example, an HTML form that calculates a dollar value over a period of time. ex: $value = $dollar_value * $duration;

If someone enters 1,000 in the dollar_value field, and 5 in the duration field, the calculated value should be 5000, but instead it calculates to 5. This is wrong.


<form method="post">
<input type="text" name="dollar_value" value="<?= @$_POST['dollar_value'] ?>" /><br />
<input type="text" name="duration" value="<?= @$_POST['duration'] ?>" /><br />
<input type="submit" />
</form>

<?php
$value = $_POST['dollar_value'] * $_POST['duration'];
echo $value;
?>
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jun 03 09:01:26 2025 UTC