Skip to content

Instantly share code, notes, and snippets.

@tannerhodges
Last active August 29, 2015 14:04
Show Gist options
  • Save tannerhodges/1b67a51186d98e3650a1 to your computer and use it in GitHub Desktop.
Save tannerhodges/1b67a51186d98e3650a1 to your computer and use it in GitHub Desktop.
Rounds value up to next quarter integer
<?php
/**
* Rounds value up to next quarter integer
* @param mixed $n
* @return float
*/
function roundUpToQuarter($n)
{
if ( ! is_numeric($n)) { return $n; }
if ($n == 0) { return null; }
$n = (float) $n;
$n *= 1000;
$x = 250;
if ($n % $x !== 0) { $n = round(($n + ($x / 2)) / $x) * $x; }
$n /= 1000;
return number_format($n, 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment