Link to home
Start Free TrialLog in
Avatar of eroute
eroute

asked on

Need file size of variable in kb/mb

For example 768 chars should come out to less then 1kb ...

thanks
Avatar of ravenpl
ravenpl
Flag of Poland image

$var = 768;
$varKB = printf("%.01 KB", $var/1024); # 0.7 KB
$varKB = printf("%.02 KB", $var/1024); # 0.76 KB
$varMB = printf("%.02 MB", $var/1048576); # 0.00 MB
$varMB = printf("%.03 MB", $var/1048576); # 0.001 MB
and so on...
Also:
$filename = "/etc/passwd";
$var = (-s $filename);
ASKER CERTIFIED SOLUTION
Avatar of ravenpl
ravenpl
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of eroute
eroute

ASKER

How do I have it auto return the best reply, example:

sub get_var_size {

      return sprintf("%.02 KB", length($_[0])/1024); # 0.76 KB
      
}

Sometimes the var length can be 6000 characters or 20000 ...

thanks
sub get_KB_size {
     return sprintf("%.2f", length($_[0])/1024); # 0.76 KB
}
argh - I need more coffee in the morning. Previously forgot about the 'f' suffix. the zero sign is here optional.
Refer http://en.wikipedia.org/wiki/Printf  http://www.phim.unibe.ch/comp_doc/c_manual/C/FUNCTIONS/format.html