Perl编号::字节::人类

问题描述

| 我正在使用此代码将字节转换为更具可读性的格式,例如155K,1.5M,1.5G,但是从CPAN的说明中无法弄清楚如何将转换后的值打印到小数点后的第二位并进行四舍五入。 谢谢。
use strict; 
use warnings;
use Number::Bytes::Human qw(format_bytes);

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

解决方法

        我可以使用Number :: Format控制小数位数:
use Number::Format qw(format_bytes);
print format_bytes(-s $file,precision => 2);
Number :: Bytes :: Human确实具有
round
选项,但我看不到用于设置精度的选项。