note 108456 modified in function.number-format by levim

From: Date: Mon, 24 Feb 2014 23:08:35 +0000
Subject: note 108456 modified in function.number-format by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-198701@lists.php.net to get a copy of this message
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. --was-- As many people state the number_format function will round. Now if your 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: 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. http://php.net/manual/en/function.number-format.php

« previous php.notes (#198701) next »