- abs: documentation ( source)
<?php
function roundNearest($decimal, $nearest){
$int = intval($decimal);
$currDecimal = $int + $nearest;
$nextDecimal = $int + 1 + $nearest;
if(abs($decimal - $currDecimal) <= abs($nextDecimal - $decimal)) return $currDecimal;
return $nextDecimal;
}
echo roundNearest('45.67', '0.43'), PHP_EOL;
echo roundNearest('34.98', '0.13'), PHP_EOL;
echo roundNearest('34.98', '0.43'), PHP_EOL;