Forum Moderators: coopster

Message Too Old, No Replies

not all locales working same with numbers

         

smallcompany

5:08 pm on Nov 3, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I setup scripts to produce money value outputs for different countries. This was in order to ensure that numbers were treated as numbers.

Below is the short version of one of the scripts (Austria) with one price value, in real life there are multiple prices in each:

<?php
// * AT Locale *
function getMoneyAT($pricesAT) {
setlocale(LC_MONETARY, 'de_AT.UTF-8');
return money_format('%n', $pricesAT);
}
function getMoneyATd($pricesAT) {
setlocale(LC_MONETARY, 'de_AT.UTF-8');
return money_format('%.0n', $pricesAT);
}
// AT current prices
$atCurrent = "1";
switch ($atCurrent) {
case "1":
$at11 = getMoneyAT(29.99);
break;
case "2":
$at11 = getMoneyAT(29.99);
break;
default:
$at11 = getMoneyAT(29.99);
}
// AT full prices
$at11f = getMoneyAT(59.99);
// AT discounts
$at11d = getMoneyATd($at11f - $at11);
?>


The above code produces PHP Warning: A non-numeric value encountered and shows zeros for discounts that are calculated at the end of the script. If I change the locale to de_DE, all works fine. Same warning happens with en_GB locale while for example pt_PT or es_ES work fine.

Why would this be? I could not find any references for such cases.

Thank you

smallcompany

6:53 pm on Nov 3, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just figured it out!

In the cases where it fails, I had to move getMoney function call into HTML. What I initially wanted was to have all in PHP scripts, so I use the least possible coding in HTML, like this:
<?php echo $at32f;?>

instead of this:
<?php echo getMoneyAT($at32f);?>


I find it confusing as if this approach did not work for any locale setting, that would not be hard to figure out. But having it working for one set, and not for another... phew.

Would anyone know why is this?

Also, what is the difference in maintaining function calls inside scripts versus using them from within HTML? Is there any reason to move those function calls outside of all scripts, no matter if locale works or not?

Thank you