Do you know an easy and straight-forward method/sub/module which allows me to convert a number (say 1234567.89) to an easily readable form – something like 1.23M?
Right now I can do this by making several comparisons, but I’m not happy with my method:
if($bytes > 1000000000){ $bytes = ( sprintf( '%0.2f', $bytes/1000000000 )). ' Gb/s'; } elsif ($bytes > 1000000){ $bytes = ( sprintf( '%0.2f', $bytes/1000000 )). ' Mb/s'; } elsif ($bytes > 1000){ $bytes = ( sprintf( '%0.2f', $bytes/1000 )). ' Kb/s'; } else{ $bytes = sprintf( '%0.2f', $bytes ). 'b/s'; }
Thank you for your help!
The Number::Bytes::Human module should be able to help you out.
An example of how to use it can be found in its synopsis: