#!/usr/bin/perl -wT
use CGI':standard';
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
print header();
print start_html(-title => "Formatted Placeholders");
print "<div style='font-size:1.2em; font-family:monospace;'>";
my $number = 3.1415926;
my $n = 248;
#formatted output
print "<div>";
printf ("%.4f", $number);
print "</div>";
#extract as a string
print "<div style='color:#cc0000;margin-top:10px;'>";
my $string = "Pi has a value of " . sprintf ("%.6f", $number);
print $string;
print "</div>";
#%lf is also a valid placeholder for real numbers as is %e or %E
print "<div style='color:#007700;margin-top:10px;'>";
printf ("%.3lf<br>", $number);
printf ("%e<br>", $number);
printf ("%E<br>", $n);
print "</div>";
#with or without leading zeros
print "<div style='color:#330033;margin-top:10px;'>";
printf ("%08.1f<br>", $number);
printf ("%.1f<br>", $number);
print "</div>";
#with or without leading zeros, and % sign
print "<div style='color:#0000ff;margin-top:10px;'>";
printf ("%010d<br>", $n);
printf ("%10d<br>", $n);
printf ("%d%%<br>", $n);
print "</div>";
#other placeholders: %b (binary), %x, %X (hexadecimal), %o (octal), %p (address)
print "<div style='color:#990099;margin-top:10px;'>";
printf ("%b<br>", $n);
printf ("%x<br>", $n);
printf ("%X<br>", $n);
printf ("%o<br>", $n);
printf ("%p<br>", $n);
print "</div>";
print "</div>";
print end_html();