note 108456 deleted from function.number-format by cmb
| From: | cmb@php.net | Date: | Sun, 08 Sep 2019 10:19:42 +0000 |
| Subject: | note 108456 deleted from function.number-format by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212129@lists.php.net to get a copy of this message | ||
Note Submitter: before_i_sleep at hotmail dot co dot uk
----
As many people state the number_format function will round. Now if you're like me and you
don't want stuff to round (and the functions below do not work for you) you can try a little
regex:
<?php
function convert_size_human($size){
$unit=array('','KB','MB','GB','TB','PB');
$byte_size = $size/pow(1024,($i=floor(log($size,1024))));
if(is_really_int($byte_size)){
return $byte_size.' '.$unit[$i];
}else{
preg_match('/^[0-9]+\.[0-9]{2}/', $byte_size, $matches);
return $matches[0].' '.$unit[$i];
}
}
?>
The regex used when the number is not an int ensures that 3.99887 GB comes out as 3.99 GB instead of
4GB as it does from number_format or the functions below.