Jump to content
Gadgetto

Not locale aware float sanitizer?

Recommended Posts

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.

 

Share this post


Link to post
Share on other sites

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');

 

Share this post


Link to post
Share on other sites
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

 

Share this post


Link to post
Share on other sites

The current float sanitizer should have options like 'localeAware' (true/false) and 'decimalSeparator' (,/.)

  • Like 2

Share this post


Link to post
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);
}

 

Share this post


Link to post
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)

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...