Welcome Guest, Not a member yet? Register   Sign In
Convert numbers question
#1

(This post was last modified: 12-23-2017, 05:21 AM by wolfgang1983.)

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"
Reply
#2

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
Neko CMS - A free Simple CMS! developed using CodeIgniter
Novi
Software Developer / Web Developer
Reply
#3

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); 
Reply
#4

(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
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#5

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

(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
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB