用perl打印浮点数
我有以下“帮助”子例程:用perl打印浮点数,perl,Perl,我有以下“帮助”子例程: sub help { print <<HELP; useage: the script will apply a constant set of changes on the code files Flags: -dest: The output directory, -source: The path to the diractory where the source code is. -ver: The
sub help {
print <<HELP;
useage: the script will apply a constant set of changes on the code files
Flags:
-dest: The output directory,
-source: The path to the diractory where the source code is.
-ver: The version of the code (default = $version)
HELP
exit 0;
}
i、 它不打印点和零
如何解决此问题?请将版本指定为字符串:
my $version = "3.0";
或者使用printf
指定打印数值时的精度
printf ( "%.1f", $version );
或者,如果您真的想对它感兴趣,可以将$version
设置为dualvar,在字符串和数字上下文中提供不同的值。(这是在炫耀——对于可维护代码来说,这可能不是一个好主意)
如果将标量指定为数值,perl会将其转换为数字<代码>3.0=3.00=3,因此它将以默认情况下所需的最小精度打印。这就是为什么你有这个问题
你可能会发现它也很有用 修复帮助字符串中目录的拼写 使用:
子帮助{
printf(使用字符串,$version=“3.0”
顺便说一句,Perl的惯例之一是版本号存储在$version
中,如:我们的$version='3.0';
printf ( "%.1f", $version );
use strict;
use warnings;
use Scalar::Util qw ( dualvar );
my $version = dualvar ( 3.0, "three point zero" );
print $version,"\n";
if ( $version >= 3.0 ) { print "3.0 or higher\n" };
sub help {
printf(<<HELP, $version);
useage: the script will apply a constant set of changes on the code files
Flags:
-dest: The output directory,
-source: The path to the directory where the source code is.
-ver: The version of the code (default = %.1f)
HELP
exit 0;
}
useage: the script will apply a constant set of changes on the code files
Flags:
-dest: The output directory,
-source: The path to the diractory where the source code is.
-ver: The version of the code (default = 3.0)