Skip to content

Instantly share code, notes, and snippets.

@bwente
Last active October 19, 2016 11:43
Show Gist options
  • Save bwente/e138188f5768fe6dd239 to your computer and use it in GitHub Desktop.
Save bwente/e138188f5768fe6dd239 to your computer and use it in GitHub Desktop.
Rounding down feature to output friendlier vague numbers
<?php
$number = floatval($input);
$optionsXpld = @explode('&', $options);
$optionsArray = array();
foreach ($optionsXpld as $xpld) {
$params = @explode('=', $xpld);
array_walk($params, create_function('&$v', '$v = trim($v);'));
if (isset($params[1])) {
$optionsArray[$params[0]] = $params[1];
} else {
$optionsArray[$params[0]] = '';
}
}
$decimals = isset($optionsArray['decimals']) ? $optionsArray['decimals'] : null;
$dec_point = isset($optionsArray['dec_point']) ? $optionsArray['dec_point'] : null;
$thousands_sep = isset($optionsArray['thousands_sep']) ? $optionsArray['thousands_sep'] : null;
$round = isset($optionsArray['round']) ? $optionsArray['round'] : null;
if (isset($optionsArray['round'])){
// $number = round($number, $round, PHP_ROUND_HALF_DOWN);
// rounding down to the nearest anything (ie. over NNN of savings)
$mod = ($number-$round)%$round;
$number = ($number-$round)+($mod<($round/2)?-$mod:$round-$mod);
}
$output = number_format($number, $decimals, $dec_point, $thousands_sep);
return $output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment