<?php
$a = array(
'21,20',
'21,00',
'21,22'
);
foreach ($a as $s) echo germanFloatVal($s). "\n";
function germanFloatVal($s) {
$n = floatval( str_replace(',', '.', $s ) );
return str_replace('.', ',', strval( $n ) );
}
Ausführen:
$ php test.php
21,2
21
21,22
Natürlich geht auch:
function germanFloatVal($s) {
return str_replace('.', ',', strval( floatval( str_replace(',', '.', $s ) ) ) );
}
Oder Du nimmst halt die einfachste Lösung:
<?php
$a = array(
'21,20',
'21,00',
'21,22'
);
foreach ($a as $s) echo rtrim ( $s , '0,') . "\n";