Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need file size of variable in kb/mb

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

thanks
0
Comment
Question by:eroute
  • 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

Technology Partners: 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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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

876 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