LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [Perl] basic byte conversion script (https://www.linuxquestions.org/questions/programming-9/%5Bperl%5D-basic-byte-conversion-script-503237/)

noir911 11-20-2006 06:12 AM

[Perl] basic byte conversion script
 
I am very new to Perl and wrote this very simple to convert bytes to KB to MB to GB. So far it's working fine. But I tried to use "%g" in place of "%f" - which according to Learning Perl 4th ed. automatically
chooses float, integer etc. But it was not giving desired output. If anyone could tell me why I can't seem to use %g and also make some comments on the current code then that would be much appreciated.
Here's the code

Code:

#!/usr/bin/perl
use strict;
use warnings;
print "enter the value\n";
chomp (my $value=<STDIN>);
my $kilo = $value/1024;
my $mega = $kilo/1024;
my $giga = $mega/1024;
print "the value is $value\n";
printf "%0.2f KB\n", $kilo;
printf "%0.2f MB\n", $mega;
printf "%0.2f GB\n", $giga;


chrism01 11-21-2006 01:01 AM

I think you'll find this page more informative: http://perldoc.perl.org/functions/sprintf.html


All times are GMT -5. The time now is 09:02 PM.