#45221 [Bgs]: Number in string conversion with comma separator
| From: | walterquez at yahoo dot com | Date: | Tue, 10 Jun 2008 13:33:50 +0000 |
| Subject: | #45221 [Bgs]: Number in string conversion with comma separator | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-125647@lists.php.net to get a copy of this message | ||
ID: 45221
User updated by: walterquez at yahoo dot com
Reported By: walterquez at yahoo dot com
Status: Bogus
Bug Type: Strings related
Operating System: Windows 2003
PHP Version: 5.2.6
New Comment:
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;
?>
Previous Comments:
------------------------------------------------------------------------
[2008-06-10 13:11:35] 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-09 18:15:34] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=45221&edit=1