IHYPRESS PROGRAMMING
Tutorials and Perl programs with code and output for beginners
Perl logo
HOME | ASP | C | CSS | GNUPLOT | HTML | JAVASCRIPT | PERL | PHP | PYTHON | RUBY | SVG
Perl ❯ Formatted Output
<15>
#!/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();
Hergestellt in Deutschland / Made in Germany
Formatted Placeholders
3.1416
Pi has a value of 3.141593
3.142
3.141593e+00
2.480000E+02
000003.1
3.1
0000000248
248
248%
11111000
f8
F8
370
ce2bd8
COPYRIGHT © 2015-2024 IHY PRESS Frankfurt am Main 60329 Deutschland