Need file size of variable in kb/mb

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

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

Expert Comment

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...
LVL 43

Expert Comment

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

Accepted Solution

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

Author Comment

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 ...

LVL 43

Expert Comment

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.

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Replace  text in a file 2 121
Regular Expression for URL 10 109
Regex rule to match two different url 5 75
combine multiple lines 2 73
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…
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…

735 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