note 124773 added to function.round
| From: | Astro@php.net | Date: | Thu, 27 Feb 2020 20:32:39 +0000 |
| Subject: | note 124773 added to function.round | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-214242@lists.php.net to get a copy of this message | ||
Okay, final version of my function:
function NumberPrecision($n, $precision=0, $is_round=true)
{
if ($is_round)
{
$r = 5 * pow(10, -($precision+1));
$n += (($n < 0) ? -$r : $r);
}
$comma = '.';
$r = 5 * pow(10, -($precision+2));
$n += (($n > 0) ? -$r : $r);
$n = number_format($n, $precision+1, $comma, '');
$n .= $comma;
list($n, $frac) = explode($comma, $n, 2);
$n = rtrim(rtrim($n, $comma) . $comma . substr($frac, 0, $precision), $comma);
return ($n);
}
It can be useful in come cases when built-in function like round() or number_format() returns
unexpected results. Works with positive and negative numbers, zero, numbers like 1/12, 0.3, numbers
in scientific notation etc.
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.195 (proxied: 46.63.38.5)
----
Manual Page -- http://php.net/manual/en/function.round.php
Edit -- https://master.php.net/note/edit/124773
Del: integrated -- https://master.php.net/note/delete/124773/integrated
Del: useless -- https://master.php.net/note/delete/124773/useless
Del: bad code -- https://master.php.net/note/delete/124773/bad+code
Del: spam -- https://master.php.net/note/delete/124773/spam
Del: non-english -- https://master.php.net/note/delete/124773/non-english
Del: in docs -- https://master.php.net/note/delete/124773/in+docs
Del: other reasons-- https://master.php.net/note/delete/124773
Reject -- https://master.php.net/note/reject/124773
Search -- https://master.php.net/manage/user-notes.php