Formatting with type float
When the variable does not have the value inside quotes, the ZEROs to the right are omitted "automatically"
$n = 100.00;
echo $n; // retorna 100
$n = 100.30;
echo $n; // retorna 100.3
When delimited by single quotation marks or double quotation marks, it is treated as a string at the time of printing. The above examples would return 100.00 and 100.30, respectively.
So if it is not important to display zero when there is a fractional value, 100.30, just do not delimit with quotation marks.
If this is not possible, a cast is required.
There are various forms that return the same result:
$n = '100.30';
echo (float)$n; // retorna 100.3
$n = '100.30';
echo floatval($n); // retorna 100.3
$n = '100.30';
echo $n + 0; // retorna 100.3
$n = '100.30';
echo +$n; // retorna 100.3
* The "cute" or "shorter" option does not mean that it is faster.
Formatting string type
If you still want to display the zero to the right of a fractional value, one option is to do a formatting that identifies that condition.
// O valor pode ser número ou string
//$n = '100.00';
//$n = 100.00;
//$n = '100.30';
$n = 100.30;
// Garante que a formatação mantenha 2 casas decimais
$n = number_format($n, 2, '.', '');
// Abstrai a parte decimal
$d = substr($n, -2);
// Se for 00, então, faz o cast para removê-los
if ($d === '00') {
$n = (float)$n;
}
// Imprime o resultado
echo $n;
More briefly, you can do just that
$n = str_replace('.00', '', number_format($n, 2, '.', ''));
echo $n;