EchoAngel has asked for the wisdom of the Perl Monks concerning the following question:

my code for example is :
my $value = "0.12345687"; $digitprecision = 20; printf STDOUT ("\n%20s", $value); printf STDOUT ("\n%" . $digitprecision . "s\n" , $value);
why wont' it work? I need a way to control the digitprecision length.
  • Comment on Perl Problems with Digit Precision Changing on the Printf Statement
  • Download Code

Replies are listed 'Best First'.
Re: Perl Problems with Digit Precision Changing on the Printf Statement
by davido (Cardinal) on Jun 24, 2004 at 15:39 UTC

    The problem is that you're stringifying the number at the wrong point. If you print the number as a string (%s20), it will pad with whitespace. If you print the number as a number, it will pad with zeros. If you want 19 decimal places, you would use something like "%.19f". Try that and you'll see what I mean.

    The documentation for sprintf explains format strings as used by sprintf and printf.

    Also, for high-precision floating point, you may find it more reliable to use Math::BigInt.


    Dave

Re: Perl Problems with Digit Precision Changing on the Printf Statement
by tinita (Parson) on Jun 24, 2004 at 15:28 UTC
    define "won't work". what do you expect, and what do you get with that code snippet?

    besides that, you might want to use the star *:
    printf STDOUT ("\n%*s\n", $digitprecision, $value); for that.

      i mean i want those two lines to output the same OUTPUT to screen. when i run
      printf STDOUT ("\n%*s\n", $digitprecision, $value);
      I get 'Use of * in printf format not supported at example.pl line 10'
        oh, i'm getting the same output (v5.8.0 linux at the moment).
        what perl version are you using?
        regarding %*s: i don't know since which perl version it is allowed...
Re: Perl Problems with Digit Precision Changing on the Printf Statement
by Roy Johnson (Monsignor) on Jun 24, 2004 at 15:55 UTC
    It works for me. The second one has a trailing newline that the first doesn't have, of course. You could also interpolate $digitprecision right in the format string:
    printf STDOUT ("\n%${digitprecision}s\n" , $value);
    Include the output you're getting from the two lines so we can see the problem.

    I do find it a little odd, talking about "digit precision" when you're telling printf that you're printing a string.


    We're not really tightening our belts, it just feels that way because we're getting fatter.
      I want the output from
      my $value = "0.03443762"; $digitprecision = 20; printf STDOUT ("\n%20s\n", $newvalue);
      I want the this printf statement to print exactly as it is
      output 0.03443762
      so how do i program this using $digitprecision = 20; I need to control the number of spacing using a variable
        Just like you did, or like I responded:
        # You did this, and it works: printf STDOUT ("\n%" . $digitprecision . "s\n", $newvalue); # I suggested this, and it works, too: printf STDOUT ("\n%${digitprecision}s\n", $newvalue);

        We're not really tightening our belts, it just feels that way because we're getting fatter.
Re: Perl Problems with Digit Precision Changing on the Printf Statement
by Fletch (Bishop) on Jun 24, 2004 at 15:40 UTC

    You need to define "doesn't work" and state what results you expected.

    freebie:~ 604> perl -le '$a="abc"x3;for $w (10, 15, 20) { printf "|%-$ +{w}s|\n", $a }' |abcabcabc | |abcabcabc | |abcabcabc |