Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need file size of variable in kb/mb

Posted on 2006-11-19
5
Medium Priority
?
231 Views
Last Modified: 2010-03-05
For example 768 chars should come out to less then 1kb ...

thanks
0
Comment
Question by:eroute
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 43

Expert Comment

by:ravenpl
ID: 17974039
$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
 
LVL 43

Expert Comment

by:ravenpl
ID: 17974047
Also:
$filename = "/etc/passwd";
$var = (-s $filename);
0
 
LVL 43

Accepted Solution

by:
ravenpl earned 2000 total points
ID: 17974091
Aeee, self correction - every printf() change to sprintf();
0
 

Author Comment

by:eroute
ID: 17974096
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
 
LVL 43

Expert Comment

by:ravenpl
ID: 17974124
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question