For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2004 > File Size Calculator









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author File Size Calculator
Silverfox

2004-08-09, 3:56 pm

Hi all, I'm trying to writing a script that will allow a user to enter a
number and that number will be converted into KB,MB or GB depending on the
size of the number. Can someone point me in the right direction?

Example:
user enter: 59443
Script will output: 58M


SilverFox
David Dorward

2004-08-09, 3:56 pm


On 9 Aug 2004, at 14:34, SilverFox wrote:

> Hi all, I'm trying to writing a script that will allow a user to enter
> a
> number and that number will be converted into KB,MB or GB depending on
> the
> size of the number. Can someone point me in the right direction?


What have you got so far? Where are you stuck? Getting user input
(where from)? Working out which order of magnitude the number is?
Converting between kilo and mega et al? Showing the output?

Show us some code.

--
David Dorward
<http://dorward.me.uk/>
<http://blog.dorward.me.uk/>

Chris Devers

2004-08-09, 3:56 pm

On Mon, 9 Aug 2004, SilverFox wrote:

> Example:
> user enter: 59443
> Script will output: 58M


I know this isn't getting into the spirit of things, but have you
considered simply using the `units` program?

% units
500 units, 54 prefixes
You have: 59443 bytes
You want: megabytes
* 0.056689262
/ 17.640025
You have: 59443 bytes
You want: kilobytes
* 59.443
/ 0.016822839
You have: ^C
% units bytes kilobytes
* 0.001
/ 1000

% units bytes megabytes
* 9.5367432e-07
/ 1048576

The nice thing about `units` -- in this context -- is that it lets the
user pick the conversion units they want to work with, and also gives
hints for converting both to & from the alternate measurement scale.

Of course, working this into a larger program that does other things
might be annoying -- in which case your way is better -- but if all you
want is the conversions, this is a solved problem :-)


--
Chris Devers
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com