note 124908 deleted from function.number-format by crell
| From: | crell@php.net | Date: | Tue, 12 Apr 2022 01:08:35 +0000 |
| Subject: | note 124908 deleted from function.number-format by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218206@lists.php.net to get a copy of this message | ||
Note Submitter: festuc at festuc dot info
----
<?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