You can simply check that the integer value is equal, that is:
$inteiro = intval($numero);
if(($numero - $inteiro) !== 0e0){
// Então adicionar $inteiro + 0.5
}
Could do something like:
function arredondar(float $numero) : string {
$inteiro = intval($numero);
$adicionar = '0';
if(($numero - $inteiro) !== 0e0){
$adicionar = '0.5' * ($numero > 0 ?: -1);
}
return bcadd($inteiro, $adicionar, 1);
}
Try this.
I do not think there's much to explain. If the result of $numero - $inteiro
is different from 0
it will add 0.5
(or -0.5
if the number is negative).
One other option, it would be easier to just concatenate .5
to integer if it falls into the condition, this would remove the need for any sum.
function arredondar(float $numero) : float {
$inteiro = intval($numero);
if(($numero - $inteiro) !== 0e0){
$inteiro .= '.5';
}
return $inteiro;
}
Test this.
If you want to round normally use round()
.