CodeIgniter Forums
Convert numbers question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Convert numbers question (/showthread.php?tid=69613)



Convert numbers question - wolfgang1983 - 12-23-2017

Hi,

I want to be able to convert my KB numbers.

For example the out put I am after lets say if my string is 471858 KB/MB then I would like it to change be able to echo to 0.04 GB

I can't seem to get it to work for round($count, 1)

What would be best way. I have tried some kb to gb converter script  like but does not convert it to like 0.04GB


PHP Code:
  function byte_convert($size) {
 
       # size smaller then 1kb
 
       if ($size 1024) return $size ' Byte';
 
       # size smaller then 1mb
 
       if ($size 1048576) return sprintf("%4.2f KB"$size/1024);
 
       # size smaller then 1gb
 
       if ($size 1073741824) return sprintf("%4.2f MB"$size/1048576);
 
       # size smaller then 1tb
 
       if ($size 1099511627776) return sprintf("%4.2f GB"$size/1073741824);
 
       # size larger then 1tb
 
       else return sprintf("%4.2f TB"$size/1073741824);
 
   



PHP Code:
$quota imap_get_quotaroot($mbox"INBOX");

$message $quota['MESSAGE'];

$percentage = ($count $message['limit']) * 100;

echo 
round($count1) . " (" round($percentage) . "%) of " $this->byte_convert($message['limit']) . " used"



RE: Convert numbers question - Kel_Novi - 12-23-2017

First convert 471858 to decimal , so that would be 0.471858 then divide it by 1024 and then you will get the result of 0.04


RE: Convert numbers question - Wouter60 - 12-23-2017

Maybe this will help:

PHP Code:
function byte_convert($size,$unit='MB',$decimals=2) {
    
    switch (
strtolower($unit)) {
        case 
'kb' :
            
$u 1024;
            break;
        case 
'mb' :
            
$u 1024**2;
            break;
        case 
'gb' :
            
$u 1024**3;
            break;
        default:
            
$u 1024**4;
    }
    return 
sprintf("%4." $decimals "f " strtoupper($unit),$size/$u);


Call the function like this:
PHP Code:
echo byte_convert(150000000,'TB',4);
echo 
byte_convert(16000); 



RE: Convert numbers question - wolfgang1983 - 12-23-2017

(12-23-2017, 06:27 AM)Wouter60 Wrote: Maybe this will help:

PHP Code:
function byte_convert($size,$unit='MB',$decimals=2) {
    
    switch (
strtolower($unit)) {
        case 
'kb' :
            
$u 1024;
            break;
        case 
'mb' :
            
$u 1024**2;
            break;
        case 
'gb' :
            
$u 1024**3;
            break;
        default:
            
$u 1024**4;
    }
    return 
sprintf("%4." $decimals "f " strtoupper($unit),$size/$u);


Call the function like this:
PHP Code:
echo byte_convert(150000000,'TB',4);
echo 
byte_convert(16000); 

I tried this $this->format_size($count, 'GB')


PHP Code:
   function format_size($size$unit 'MB'$decimals 2) {
 
      
        switch 
(strtolower($unit)) {
 
           case 'kb' :
 
               $u 1024;
 
               break;
 
           case 'mb' :
 
               $u 1024**2;
 
               break;
 
           case 'gb' :
 
               $u 1024**3;
 
               break;
 
           default:
 
               $u 1024**4;
 
       }

 
       return sprintf("%4." $decimals "f " strtoupper($unit),$size/$u);
 
   

My all the total of my email sizes are 471858

But only returns 0.00 GB not 0.04 GB


RE: Convert numbers question - PaulD - 12-23-2017

471858 bytes is 0.0004 GB. So the function is returning the correct figure to 2 decimal places.


RE: Convert numbers question - wolfgang1983 - 12-23-2017

(12-23-2017, 03:54 PM)PaulD Wrote: 471858 bytes is 0.0004 GB. So the function is returning the correct figure to 2 decimal places.

OK so will have to change a couple of things cool bit more practice for me