php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41681 filter_validate_float doesn't allow scientific flag
Submitted: 2007-06-13 18:31 UTC Modified: 2007-06-14 13:25 UTC
From: rich at corephp dot co dot uk Assigned: pajoye (profile)
Status: Closed Package: Filter related
PHP Version: 5.2.3 OS: Windows XP SP2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
40 + 3 = ?
Subscribe to this entry?

 
 [2007-06-13 18:31 UTC] rich at corephp dot co dot uk
Description:
------------
FILTER_VALIDATE_FLOAT doesn't allow for the use of FILTER_FLAG_ALLOW_SCIENTIFIC as an option, although it DOES allow for the use of FILTER_FLAG_ALLOW_THOUSAND.

As both are valid FILTER_SANITIZE_NUMBER_FLOAT flags, why not the same for the validate float filter itself?

Reproduce code:
---------------
$val = '34,200';
$result = filter_var($val, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND);
var_dump($val);
var_dump($result);

$val = 'e34200';
$result = filter_var($val, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_SCIENTIFIC);
var_dump($val);
var_dump($result);


Expected result:
----------------
string(6) "34,200"
float(34200)

string(6) "e34200"
float(34200)


Actual result:
--------------
string(6) "34,200"
float(34200)

string(6) "e34200"
bool(false)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-06-13 20:45 UTC] pajoye@php.net
It is a flag for the sanitizing function not the validate filter, see the main page of the filter manual function. 
(Not a bug > bogus)
 [2007-06-13 23:22 UTC] rich at corephp dot co dot uk
I understand that, in which case why does the "allow thousands" flag work? One way or the other it is inconsistent. Either the bug is that the allow thousands flag works, or the bug is that allow scientific doesn't.
 [2007-06-14 05:30 UTC] pajoye@php.net
"In which case why does the "allow thousands flag work"

Because the thousand separator can be , ' or even . There is also an option to define it.

The scientific format is support by default, always. Just like a (float)$var.

Finally, "e34200" is not a valid scientific format. Please refer to the various tests cases in the filter extensions to see other examples (http://cvs.php.net/viewvc.cgi/php-src/ext/filter/tests/).
 [2007-06-14 07:53 UTC] rich at corephp dot co dot uk
Then the 'bug' is simply in the documentation, because the thousands flag is *not* listed as a valid flag for that filter, and according to what you said, it should be.
 [2007-06-14 08:04 UTC] pajoye@php.net
"Then the 'bug' is simply in the documentation, because the thousands
flag is *not* listed as a valid flag for that filter, and according to
what you said, it should be."

Good point, and the decimal option was missing as well. It is fixed in CVS, it will show up in the next manual update.

Thanks for your report!
 [2007-06-14 13:09 UTC] rich at corephp dot co dot uk
By 'the decimal option was missing as well' do you mean the FILTER_FLAG_ALLOW_FRACTION flag? If so I have found that using that makes no difference at all:

$val = 33.4;
$result = filter_var($val, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
var_dump($val);
var_dump($result);

$val = 4.99;
$result = filter_var($val, FILTER_VALIDATE_FLOAT);
var_dump($val);
var_dump($result);

The first allows decimals and the float is created. The second doesn't specify decimals, but the float is still created. Using the German decimal notation (3,99) causes a boolean(false) regardless of the allow_fraction flag being present or not.

Unless there is another flag you meant?
 [2007-06-14 13:25 UTC] pajoye@php.net
"By 'the decimal option was missing as well' do you mean the
FILTER_FLAG_ALLOW_FRACTION flag?"

I meant the decimal separator. You can define which character has to be used as decimal separator. See the test case 031.phpt for examples (links pasted earlier).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 02:01:29 2024 UTC