note 124908 added to function.number-format

From: Date: Mon, 13 Apr 2020 08:42:11 +0000
Subject: note 124908 added to function.number-format
Groups: php.notes 
Request: Send a blank email to php-notes+get-214466@lists.php.net to get a copy of this message
<?php // by www.festuc.info // If you need trim unnecessary right zeros part of a string to decimal number, // for a human readable you can trim it with this little hack. function number_format_rtrim($value,$maxdecimal=0,$decimalsing='.',$thousandsing=','){ if (is_numeric($value)){ $value=number_format($value,$maxdecimal,'.','');//to change input type to string $i=-1; if (strpos($value,'.'))while (substr($value,$i)==0){ $i--; $maxdecimal--; } else $maxdecimal=0; return number_format($value,$maxdecimal,$decimalsing,$thousandsing); } } $decimalsing=','; //french mode $thousandsing=' '; $a = array (10000/3,10.15000,'58.0000',1.01000,'pastafarism','1001.00100',30.05,25/2,'0.2500','potato'); foreach ($a as $w){ unset($o); $o=number_format_rtrim($w,5,$decimalsing,$thousandsing); if(isset($o)) echo "$w: $o"; else echo "$w is not a number"; echo '<br/>'; } ?> This is the output 3333.3333333333: 3 333,33333 10.15: 10,15 58.0000: 58 1.01: 1,01 pastafarism is not a number 1001.00100: 1 001,001 30.05: 30,05 12.5: 12,5 0.2500: 0,25 potato is not a number ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.195 (proxied: 81.39.193.180) ---- Manual Page -- http://php.net/manual/en/function.number-format.php Edit -- https://master.php.net/note/edit/124908 Del: integrated -- https://master.php.net/note/delete/124908/integrated Del: useless -- https://master.php.net/note/delete/124908/useless Del: bad code -- https://master.php.net/note/delete/124908/bad+code Del: spam -- https://master.php.net/note/delete/124908/spam Del: non-english -- https://master.php.net/note/delete/124908/non-english Del: in docs -- https://master.php.net/note/delete/124908/in+docs Del: other reasons-- https://master.php.net/note/delete/124908 Reject -- https://master.php.net/note/reject/124908 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#214466) next »