php german locale + float to string conversion: comma and not dot

raw

README.rst

Problem:

$ php -a
Interactive mode enabled

php > echo (string) 2.5;
2.5

php > setlocale(LC_ALL, 'de_DE.UTF-8');
php > echo (string) 2.5;
2,5

php > echo (string) var_export(2.5,true);
2.5
php > echo (string) var_export('2.5', true);
'2.5'
raw

safefloatstr.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
/**
 * Cast variable to string.
 * Floats get converted correctly without locale problems (always a dot)
 *
 * @param mixed $value
 *
 * @return string
 */
function safefloatstr($value)
{
    if (is_float($value)) {
        return var_export($value, true);
    }
 
    return (string) $value;
}
?>
Christian Weiske Christian Weiske
owner

History