|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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 />
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 21:00:02 2025 UTC |
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' => ',')));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);