PHP: locale-dependent float to string cast -
i'm sitting on machine en_us locale , piece of php code
setlocale(lc_all,'de_de.utf8'); var_dump((string)1.234);
returns
string(5) "1.234"
whereas on colleague's machine has german locale, returns
string(5) "1,234"
why heck php use locale when typecasting floats strings? how can disable it? i'd have function return string(5) "1.234" on machines, regardless of locale settings.
secondly , less important: why php ignore setlocale on machine?
why heck php use locale when typecasting floats strings?
that's it's behaviour
how can disable it?
you can't (as far know).
you may set locale en_us
if have locale installed.
i'd have function return string(5) "1.234" on machines, regardless of locale settings.
you have 2 options:
1) number_format(1.234, 3, '.', '');
2) sprintf('%.3f', 1.234);
in both cases have specify how may decimal digits want.
in second case may not specify them , default value of 6.
if don't want trailing zeroes may trim
them.
secondly , less important: why php ignore setlocale on machine?
as devzer0 commented may not have locale installed.
Comments
Post a Comment