Debido a esta pregunta es vieja. Primero, lo siento por esto.
La pregunta es sobre el número xxx.xx, pero en caso de que sea x, xxx.xxxxx o un separador decimal de diferencia como xxxx, xxxx, esto puede ser más difícil de encontrar y eliminar dígitos cero del 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;
}
Y aquí está el código para la prueba.
$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, ','));
}