Jump to content

Not locale aware float sanitizer?


Gadgetto
 Share

Recommended Posts

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...