Devido a esta pergunta é antiga. Primeiro, me desculpe por isso.
A pergunta é sobre o número xxx.xx, mas, no caso de ser x, xxx.xxxxx ou separador decimal com diferença como xxxx, xxxx, pode ser mais difícil encontrar e remover dígitos zero do valor decimal.
/**
* Remove zero digits from decimal value.
*
* @param string|int|float $number The number can be any format, any where use in the world such as 123, 1,234.56, 1234.56789, 12.345,67, -98,765.43
* @param string The decimal separator. You have to set this parameter to exactly what it is. For example: in Europe it is mostly use "," instead of ".".
* @return string Return removed zero digits from decimal value.
*/
function removeZeroDigitsFromDecimal($number, $decimal_sep = '.')
{
$explode_num = explode($decimal_sep, $number);
if (is_array($explode_num) && isset($explode_num[count($explode_num)-1]) && intval($explode_num[count($explode_num)-1]) === 0) {
unset($explode_num[count($explode_num)-1]);
$number = implode($decimal_sep, $explode_num);
}
unset($explode_num);
return (string) $number;
}
E aqui está o código para teste.
$numbers = [
1234,// 1234
-1234,// -1234
'12,345.67890',// 12,345.67890
'-12,345,678.901234',// -12,345,678.901234
'12345.000000',// 12345
'-12345.000000',// -12345
'12,345.000000',// 12,345
'-12,345.000000000',// -12,345
];
foreach ($numbers as $number) {
var_dump(removeZeroDigitsFromDecimal($number));
}
echo '<hr>'."\n\n\n";
$numbers = [
1234,// 12324
-1234,// -1234
'12.345,67890',// 12.345,67890
'-12.345.678,901234',// -12.345.678,901234
'12345,000000',// 12345
'-12345,000000',// -12345
'12.345,000000',// 12.345
'-12.345,000000000',// -12.345
'-12.345,000000,000',// -12.345,000000 STRANGE!! but also work.
];
foreach ($numbers as $number) {
var_dump(removeZeroDigitsFromDecimal($number, ','));
}