in reply to Why do I get random numbers?
... manipulate the formatting of a number ... %.{$var}f does the trick.
Another way is the printf * format specifier:
See sprintf for a discussion of all format specifiers.c:\@Work\Perl\monks>perl -wMstrict -le "my $p = 30; my $n = 1.111111111111111111111111111111; ;; printf '%0.*f', $p, $n; " 1.111111111111111200000000000000
Update: Actually, %.{$var}f doesn't do any trick except to produce an "Invalid conversion in printf..." warning. But the syntax is correct in the OPed example code:
printf( "%.${dig}f", $num );
Give a man a fish: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Why do I get random numbers?
by Cleggy (Initiate) on Jun 15, 2018 at 08:45 UTC | |
by AnomalousMonk (Archbishop) on Jun 15, 2018 at 14:57 UTC |