I have a random number from 0.001 to 1000, and I need perl to print with a fixed column width of 5 characters. That is, if it is too long, it should be rounded, and if it is too short, it should be filled with spaces.
Everything I found on the Internet suggested using sprintf, but sprintf ignores the field width if the number is too large.
Is there any way to get perl for this?
What does not work:
my $number = 0.001 + rand(1000);
my $formattednumber = sprintf("%5f", $number);
print <<EOF;
$formattednumber
EOF
source
share