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

Is it posible to set a variable from the following printf statement?
#!/usr/bin/perl use warnings; use strict; #hex -> dec: function hex print hex ('0x10000a0f'), "\n"; #dec -> hex: s/printf with %x my $val = printf "0x" . "%x\n", 268438031; print "VAL: $val\n";

Replies are listed 'Best First'.
Re: setting a variable from printf
by cfreak (Chaplain) on Jul 07, 2003 at 13:37 UTC
      ah yes...Thank you!
      one more question,
      Is it possible to pass
      my $dec = sprintf hex('0x10000a0f'), "\n";
      a variable like so:
      my $dec = sprintf hex($hex_val), "\n";
      Thanks again!
Re: setting a variable from printf
by tos (Deacon) on Jul 07, 2003 at 13:51 UTC
    Use sprintf ...
      sprintf FORMAT, LIST

      Returns a string formatted by the usual printf conventions of the C library function sprintf.

      For example:

      # Format number with up to 8 leading zeroes
      $result = sprintf("%08d", $number);

      # Round number to 3 digits after decimal point
      $rounded = sprintf("%.3f", $number);