php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39158 FILTER_VALIDATE_FLOAT doesn't uses decimal_point from locale
Submitted: 2006-10-14 10:32 UTC Modified: 2006-10-14 15:41 UTC
From: andiesPostfach at web dot de Assigned:
Status: Not a bug Package: Filter related
PHP Version: 5.2.0RC5 OS: Suse Linux 9.3
Private report: No CVE-ID: None
 [2006-10-14 10:32 UTC] andiesPostfach at web dot de
Description:
------------
FILTER_VALIDATE_FLOAT should use the decimal point info from the given locale. In my case (in Germany) it should use ','.


Reproduce code:
---------------
  $loc_de = setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'deu_deu');
  echo "Preferred locale for german on this system is '$loc_de'";

  $locale_info = localeconv();
  print_r($locale_info);
  
  // This should work an give 0,27
  $value = "0,27";
  echo "Value is: ".filter_var( $value, FILTER_VALIDATE_FLOAT );
  
  // This shouldn't work but it gives 0,27
  $value = "0.27";
  echo "Value is: ".filter_var( $value, FILTER_VALIDATE_FLOAT );
  

Expected result:
----------------
Preferred locale for german on this system is 'de_DE@euro'Array
(
    [decimal_point] => ,
    [thousands_sep] => .
    [int_curr_symbol] => EUR 
    [currency_symbol] => ?
    [mon_decimal_point] => ,
    [mon_thousands_sep] => .
    [positive_sign] => 
    [negative_sign] => -
    [int_frac_digits] => 2
    [frac_digits] => 2
    [p_cs_precedes] => 0
    [p_sep_by_space] => 1
    [n_cs_precedes] => 0
    [n_sep_by_space] => 1
    [p_sign_posn] => 1
    [n_sign_posn] => 1
    [grouping] => Array
        (
            [0] => 3
            [1] => 3
        )

    [mon_grouping] => Array
        (
            [0] => 3
            [1] => 3
        )

)
Value for 0,27 is: 0,27<br />
Value for 0.27 is: <br />

Actual result:
--------------
Preferred locale for german on this system is 'de_DE@euro'Array
(
    [decimal_point] => ,
    [thousands_sep] => .
    [int_curr_symbol] => EUR 
    [currency_symbol] => ?
    [mon_decimal_point] => ,
    [mon_thousands_sep] => .
    [positive_sign] => 
    [negative_sign] => -
    [int_frac_digits] => 2
    [frac_digits] => 2
    [p_cs_precedes] => 0
    [p_sep_by_space] => 1
    [n_cs_precedes] => 0
    [n_sep_by_space] => 1
    [p_sign_posn] => 1
    [n_sign_posn] => 1
    [grouping] => Array
        (
            [0] => 3
            [1] => 3
        )

    [mon_grouping] => Array
        (
            [0] => 3
            [1] => 3
        )

)
Value for 0,27 is: <br />
Value for 0.27 is: 0,27<br />

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-10-14 10:55 UTC] tony2001@php.net
Both "." and "," are allowed if you didn't specify your own.
This is expected behaviour. 
 [2006-10-14 14:38 UTC] andiesPostfach at web dot de
Obviously ',' isn't allowed !
 [2006-10-14 15:41 UTC] pajoye@php.net
It does not use the LOCALE as it filters external data (from browsers or other clients). The default decimal separator is '.'. You have to use the "decimal" filter option to define ',' as decimal operator:
$out = filter_var($float, FILTER_VALIDATE_FLOAT, array("options"=>array('decimal' => ',')));
 [2019-12-15 16:03 UTC] drtechno at mail dot com
like above (according to pajoye@php.net) correct, and the text book way:
$out = filter_var($float, FILTER_VALIDATE_FLOAT, array("options"=>array('decimal' => ',')));

or use this one since the character is the same....

$out=filter_var($float, FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_THOUSAND);
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Nov 28 00:03:13 2021 UTC