note 124968 added to function.number-format

From: Date: Thu, 30 Apr 2020 14:48:10 +0000
Subject: note 124968 added to function.number-format
Groups: php.notes 
Request: Send a blank email to php-notes+get-214564@lists.php.net to get a copy of this message
If you want a number of digits after the point, but not unnecessary zeros. Eg. number_format(1.20000,4) = 1.2000 num_format(1.20000,4,0) = 1.2 number_format(1.20000,4) = 1.2000 num_format(1.20000,4,2) = 1.20 number_format(1.23456,4) = 1.2345 num_format(1.23456,4,2) = 1.2345 function num_format($numVal,$afterPoint=2,$minAfterPoint=0,$thousandSep=",",$decPoint="."){ // Same as number_format() but without unnecessary zeros. $ret = number_format($numVal,$afterPoint,$decPoint,$thousandSep); if($afterPoint!=$minAfterPoint){ while(($afterPoint>$minAfterPoint) && (substr($ret,-1) =="0") ){ // $minAfterPoint!=$minAfterPoint and number ends with a '0' // Remove '0' from end of string and set $afterPoint=$afterPoint-1 $ret = substr($ret,0,-1); $afterPoint = $afterPoint-1; } } if(substr($ret,-1)==$decPoint) {$ret = substr($ret,0,-1);} return $ret; } ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.195 (proxied: 2a02:c7d:3299:5300:1597:23c2:62cf:dfa3) ---- Manual Page -- http://php.net/manual/en/function.number-format.php Edit -- https://master.php.net/note/edit/124968 Del: integrated -- https://master.php.net/note/delete/124968/integrated Del: useless -- https://master.php.net/note/delete/124968/useless Del: bad code -- https://master.php.net/note/delete/124968/bad+code Del: spam -- https://master.php.net/note/delete/124968/spam Del: non-english -- https://master.php.net/note/delete/124968/non-english Del: in docs -- https://master.php.net/note/delete/124968/in+docs Del: other reasons-- https://master.php.net/note/delete/124968 Reject -- https://master.php.net/note/reject/124968 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#214564) next »