<?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;
- Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.13
- 45.43
35.13
35.43
preferences:
65.5 ms | 406 KiB | 5 Q