Gadgetto Posted December 13, 2019 Share Posted December 13, 2019 Is there a sanitizer like $sanitizer->float($value) which is not local aware? I'd need return values which are float but always with . (dot) as decimal separator. Link to comment Share on other sites More sharing options...
interrobang Posted December 13, 2019 Share Posted December 13, 2019 I think this would be a useful addition to the Sanitizer class. Something like this should do it: $dotfloat = rtrim(number_format($number,100,'.',''), '.0'); Link to comment Share on other sites More sharing options...
Gadgetto Posted December 13, 2019 Author Share Posted December 13, 2019 7 minutes ago, interrobang said: I think this would be a useful addition to the Sanitizer class. Something like this should do it: $dotfloat = rtrim(number_format($number,100,'.',''), '.0'); Hmm... 99.99 --> 99.9899999999999948840923025272786617279052734375 99,99 --> 99 990 --> 99 0.99 --> 0.9899999999999999911182158029987476766109466552734375 Link to comment Share on other sites More sharing options...
Gadgetto Posted December 13, 2019 Author Share Posted December 13, 2019 The current float sanitizer should have options like 'localeAware' (true/false) and 'decimalSeparator' (,/.) 2 Link to comment Share on other sites More sharing options...
interrobang Posted December 13, 2019 Share Posted December 13, 2019 WTF?! Sorry, you are right, this is not useful. Link to comment Share on other sites More sharing options...
Robin S Posted December 13, 2019 Share Posted December 13, 2019 This maybe: https://www.php.net/manual/en/function.floatval.php#92563 function ParseFloat($floatString){ $LocaleInfo = localeconv(); $floatString = str_replace($LocaleInfo["mon_thousands_sep"] , "", $floatString); $floatString = str_replace($LocaleInfo["mon_decimal_point"] , ".", $floatString); return floatval($floatString); } Link to comment Share on other sites More sharing options...
Gadgetto Posted December 14, 2019 Author Share Posted December 14, 2019 9 hours ago, Robin S said: This maybe: https://www.php.net/manual/en/function.floatval.php#92563 function ParseFloat($floatString){ $LocaleInfo = localeconv(); $floatString = str_replace($LocaleInfo["mon_thousands_sep"] , "", $floatString); $floatString = str_replace($LocaleInfo["mon_decimal_point"] , ".", $floatString); return floatval($floatString); } This is also locale aware as floatval will return a float number with either a . (dot) or a , (comma) as decimal separator depending on your locale setting. This behavior is perfectly covered by $sanitizer->float. The problem is, I need a float sanitizer which always returns a float number with . as dezimal separator - but it shouldn't be a string which will be returned (so str_replace drops out) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now