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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...