Cleggy has asked for the wisdom of the Perl Monks concerning the following question:
So, I wanted to understand the printf command and how to manipulate the formatting of a number (yes I am that new to this), namely how to insert a variable into %.f it would seem %.{$var}f does the trick.
And In this small script I thought I had it worked out
If I enter 1.111111111111111111111111111111 (1 with 30 1s after the decimal point) as an experimental value, when asked 'How many digits would you like after the decimal point ? ' if I answer 2 I get 1.11 as expected.
If I answer 10 I get 1.1111111111 as I would expect.
However if I answer 20 I get 1.11111111111111116045 (?)
And if I answer 30 I get 1.111111111111111160454356650007 (?)
Can someone tell me why please.
#! /usr/bin/perl -w # experiment with printf use strict; my $num = 0; my $dig = 0; print 'Input a number :'; chomp( $num = <STDIN> ); print 'How many digits would you like after the decimal point ? '; chomp( $dig = <STDIN> ); print "\n"; print 'Original number was '; print "$num \n"; printf("And to $dig, points is "); printf( "%.${dig}f", $num ); print "\n\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Why do I get random numbers?
by choroba (Cardinal) on Jun 14, 2018 at 22:18 UTC | |
|
Re: Why do I get random numbers?
by AnomalousMonk (Archbishop) on Jun 14, 2018 at 22:36 UTC | |
by Cleggy (Initiate) on Jun 15, 2018 at 08:45 UTC | |
by AnomalousMonk (Archbishop) on Jun 15, 2018 at 14:57 UTC | |
|
Re: Why do I get random numbers?
by syphilis (Archbishop) on Jun 15, 2018 at 00:51 UTC | |
|
Re: Why do I get random numbers?
by hippo (Archbishop) on Jun 14, 2018 at 23:06 UTC | |
|
Re: Why do I get random numbers?
by Anonymous Monk on Jun 15, 2018 at 13:08 UTC |