3v4l.org

run code in 300+ PHP versions simultaneously
<?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;

preferences:
25.12 ms | 404 KiB | 5 Q