Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

Need file size of variable in kb/mb

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

thanks
0
eroute
Asked:
eroute
  • 4
1 Solution
 
ravenplCommented:
$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...
0
 
ravenplCommented:
Also:
$filename = "/etc/passwd";
$var = (-s $filename);
0
 
ravenplCommented:
Aeee, self correction - every printf() change to sprintf();
0
 
erouteAuthor Commented:
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
0
 
ravenplCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now