note 80760 deleted from function.number-format by levim
| From: | levim@php.net | Date: | Sun, 23 Mar 2014 14:12:17 +0000 |
| Subject: | note 80760 deleted from function.number-format by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-199019@lists.php.net to get a copy of this message | ||
Note Submitter: eregon at msn dot com
----
I have seen some scripts here for format a filesize, but i think this can be useful:
<?php
function format_filesize($number, $decimals = 3, $force_unit = false, $dec_char = ',',
$thousands_char = ' ')
{//string format_filesize(int(0,) $number, (bool(0), int(0,4)) $force_unit, int $decimals, char
$dec_char, char $thousands_char)
//format a filesize $number with unit (setted by $force_unit(see below for the number))
$units = array('o', 'Ko', 'Mo', 'Go', 'To');
if($force_unit === false)
$unit = floor(log($number, 2) / 10);
else
$unit = $force_unit;
if($unit == 0)
$decimals = 0;
return number_format($number / pow(1024, $unit), $decimals, $dec_char, $thousands_char).'
'.$units[$unit];
}
?>
ex:
format_filesize(2540367) => '2,423 Mo'
format_filesize(2540367, 2) => '2,42 Mo'
format_filesize(2540367, 1, 1) => '2 480,8 Ko' //0=>'o',
1=>'Ko', 2=>'Mo', 3=>'Go', 4=>'To' (see $units)
format_filesize(2540687367, 2, 1, ',', '.') => '2.481.140,01 Ko'
log($number, 2) / 10 = log($number, 1024) ;)
Fast & Easy, isn't it? :)