- I have the below Perl function to display up to two decimals places. It’s not working when the input value is 2.01, and it gives the output as 2 instead of 2.01. I am not sure why it’s rounding.
Instead of printf I wrote the output to a file, but still it gives me output1 as 2.
my $ramount = 2.01;
$ramount = int($ramount*100)/100;
printf "output1: $ramount";
- If I have values like .2, .23, .2345, 1,23, 23.1, and 9, what function can I use to pad zeros so that it displays 0.2, 0.23, 0.2345, 1, 23, 23.1, and 9?
I think this sequence will answer your question:
Essentially (and this has been answered many times on SO), 2.01 cannot be represented EXACTLY as a floating point number. The closest possible float is, as you see above, 2.009999999999999716…
As to padding, try
The leading zero in the format tells
printf(orsprintf) to left-pad with zero.