Solved

Need file size of variable in kb/mb

Posted on 2006-11-19
5
179 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 500 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now