perl - Converting text MB/GB to value -
i have variable contains file size:
my $tx = "41.4 mb";
or
my $tx = "34.4 gb";
how go converting kb value. if tx contains mb * 1024, , if tx contains gb * 1024 * 1024?
you need separate out , test units.
use strict; use warnings; sub size_to_kb { $size = shift; ($num, $units) = split ' ', $size; if ($units eq 'mb') { $num *= 1024; } elsif ($units eq 'gb') { $num *= 1024 ** 2; } elsif ($units ne 'kb') { die "unrecognized units: $units" } return "$num kb"; } print size_to_kb("41.4 mb"), "\n"; print size_to_kb("34.4 gb"), "\n";
outputs:
42393.6 kb 36071014.4 kb
< / hand holding >
Comments
Post a Comment