And the clouds parted, and SilverFox said... > > 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 >
Here's a little chunk that should give you about what you're looking for, up to Tebibytes (2**40 bytes). Note that I used the "binary" prefixes[1] (Kibi, Mebi, Gibi, Tebi) as opposed to the base-10 versions (Kilo, Mega, Giga, Tera). Feel free to change them if you're so inclined. :) --- Begin Chunk --- our %ByteCount = ( B => 1, KiB => 2**10, MiB => 2**20, GiB => 2**30, TiB => 2**40 ); sub prettybyte { my $bytes = shift; foreach my $unit ( qw{ TiB GiB MiB KiB B } ) { if ($bytes >= $ByteCount{$unit}) { return sprintf("%4.3f $unit", $bytes/$ByteCount{$unit}); } } } --- End Chunk --- HTH[2]- Brian [1] http://www.alcyone.com/max/reference/physics/binary.html -anyone remember offhand the URL to the /. story on these, btw? [2] I'm a little rushed at the moment, so I don't have time to fill in any details of how it works. Let me know if you want/need an explanation and I'll be happy to provide one. :) /~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ | Brian Gerard Some drink at the fountain of | | First initial + 'lists' knowledge...others just gargle. | | at technobrat dot com | \______________________________________________________________________/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>