perl

How to print second digit after decimal point with Number::Bytes::Human?


I'm using this code to convert bytes into more readable format, e.g. 155K, 1.5M, 1.5G, but can't figure out from the explanation on CPAN how the converted values to be printed to the second digit after the decimal point and to be rounded.

use strict; 
use warnings;
use Number::Bytes::Human qw(format_bytes);

my $bytes = format_bytes(-s $file);

Solution

  • I am able to control the number of decimals using Number::Format:

    use Number::Format qw(format_bytes);
    print format_bytes(-s $file, precision => 2);
    

    Number::Bytes::Human does have a round option, but I do not see an option to set the precision.