Skip to content

Instantly share code, notes, and snippets.

@gcatlin
Created July 7, 2010 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcatlin/466587 to your computer and use it in GitHub Desktop.
Save gcatlin/466587 to your computer and use it in GitHub Desktop.
Number rounding for chart scales
<?php
function nice_number($num, $round=false) {
$m = pow(10, floor(log10($num)));
if ($m == 0) {
return 0;
}
$f = $num / $m;
if ($round) {
if ($f < 1.5) $nf = 1;
elseif ($f < 3) $nf = 2;
elseif ($f < 7) $nf = 5;
else $nf = 10;
} else {
if ($f <= 1) $nf = 1;
elseif ($f <= 2) $nf = 2;
elseif ($f <= 5) $nf = 5;
else $nf = 10;
}
return $nf * $m;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment