php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #57184 input_get float decimal option
Submitted: 2006-08-18 05:28 UTC Modified: 2006-08-18 21:53 UTC
From: x dot philbert at pixandlog dot net Assigned: pajoye (profile)
Status: Not a bug Package: filter (PECL)
PHP Version: 5.1.4 OS: Gentoo Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: x dot philbert at pixandlog dot net
New email:
PHP Version: OS:

 

 [2006-08-18 05:28 UTC] x dot philbert at pixandlog dot net
Description:
------------
Hello.
With decimal option, it's not possible to check two 
differents float.
Users type either '42.42' or '42,42'. But it's not easy to 
force user to type one style of float.
How use decimal option with mutli parameters ?
But with separator flag how do you do difference ?
Example : '1,597,541.42' english style ?
'5 999 741,487' french style ?
'987412.42' user style ;)
'4870,42' user style ;)
'4, 454, 454. 42' other style.
It's not easy to determine the right way.
Thank you for your job.


Reproduce code:
---------------
<?php
//$_GET['test1'] = '42,42';
//$_GET['test2'] = '42.42';
$test1 = input_get(INPUT_GET, 'test1', FILTER_VALIDATE_FLOAT, array('decimal' => ','));
var_dump($test1);
$test2 = input_get(INPUT_GET, 'test2', FILTER_VALIDATE_FLOAT);
var_dump($test2);
$test1 = input_get(INPUT_GET, 'test1', FILTER_VALIDATE_FLOAT);
var_dump($test1);
$test2 = input_get(INPUT_GET, 'test2', FILTER_VALIDATE_FLOAT, array('decimal' => ','));
var_dump($test2);
?>


Expected result:
----------------
float(42.42);
float(42.42);
float(42.42);
float(42.42);


Actual result:
--------------
float(42.42);
float(42.42);
bool(false);
bool(false);


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-08-18 21:53 UTC] pierre dot php at gmail dot com
It is not possible to allow many separators for one call. You have to define it on each call.

About telling the users which format to use, it is pretty much the same problem for every input (dates for example). You have to choose one and tell the user to use it. There is no magical solution,  autodetection always ends to troubles, is it a mistake? is it what the users really want? etc.
 [2006-08-21 05:16 UTC] x dot philbert at pixandlog dot com
Yes.
It s very hard to allow many separators for one call.
But i find on forum this help. I write this comment 
because it's a good way for check locales informations. 
This code is a part of Zend Framework.

/**
 * Returns value if it is a valid float value, FALSE 
otherwise. 
 * 
 * @param mixed $value 
 * @return mixed 
 */ 
public static function isFloat($value) 
{ 
    $locale = localeconv(); 
    $value = str_replace($locale['decimal_point'], '.', 
$value); 
    $value = str_replace($locale['thousands_sep'], '', 
$value); 
    return (strval(floatval($value)) == $value); 
}
Why don't verif locale value with localeconv in C code ?
Good work, continue please for the next release.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 16:01:29 2024 UTC