note 124758 added to function.round

From: Date: Tue, 25 Feb 2020 14:01:45 +0000
Subject: note 124758 added to function.round
Groups: php.notes 
Request: Send a blank email to php-notes+get-214223@lists.php.net to get a copy of this message
Sorry, my previous function doesn't work with negative numbers and zero (and this wasn't my case). If you works with negative numbers and zero you could try this: NumberPrecision($n, $precision=0, $is_round=true) { $r = (($is_round) ? 5 : 1); $r *= pow(10, -($precision+1)); $n += (($n < 0) ? -$r : $r); $comma = '.'; $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); } Also this works with numbers like 0.3 and numbers with exponent. Don't forget to test it for your cases. ---- 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/124758 Del: integrated -- https://master.php.net/note/delete/124758/integrated Del: useless -- https://master.php.net/note/delete/124758/useless Del: bad code -- https://master.php.net/note/delete/124758/bad+code Del: spam -- https://master.php.net/note/delete/124758/spam Del: non-english -- https://master.php.net/note/delete/124758/non-english Del: in docs -- https://master.php.net/note/delete/124758/in+docs Del: other reasons-- https://master.php.net/note/delete/124758 Reject -- https://master.php.net/note/reject/124758 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#214223) next »