Skip to content

Instantly share code, notes, and snippets.

@rnstux
Created June 15, 2020 03:47
Show Gist options
  • Save rnstux/28f70162d3a380f71251955ad5cc76b2 to your computer and use it in GitHub Desktop.
Save rnstux/28f70162d3a380f71251955ad5cc76b2 to your computer and use it in GitHub Desktop.
<?php
$importe = 32887.76 * (10.6666 / 100);
echo $importe;
echo "\n";
echo floor($importe * 10000) / 10000;
echo "\n";
echo money_format_rounded('%!i', $importe);
echo "\n";
function money_format_rounded($format, $number, $maxPrecision = 2, $roundingType = \PHP_ROUND_HALF_UP)
{
$strlen = strlen($number);
if ($strlen === 0) {
return money_format($format, $number);
}
$length = $strlen - strrpos($number, '.') - 1;
if ($length <= 0) {
return money_format($format, $number);
}
$rounded = $number;
for ($i = --$length; $i >= $maxPrecision; $i--) {
$rounded = round($rounded, $i, $roundingType);
}
return money_format($format, $rounded);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment