Is there an actual package in CPAN to convert such string:
my $string = "54.4M"
my $string2 = "3.2G"
into the actual number in bytes:
54,400,000
3,200,000,000
And vice versa.
In principle what I want to do at the end is to sum out all the memory size.
To get the exact output you asked for, use Number::FormatEng and Number::Format:
By the way, only
unformat_prefis needed if you are going to perform calculations with the result.Since Number::FormatEng was intended for engineering notation conversion (not for bytes), its prefix is case-sensitive. If you want to use it for kilobytes, you must use lower case
k.Number::Format will convert these strings into actual bytes (kinda, almost).
The reason I said “kinda, almost” is that
Number::Formattreats1Kas being equal to 1024 bytes, not 1000 bytes. That’s probably why it gives a weird-looking result (with fractional bytes), unless it is rounded.