Text Formatting and Date Strings
Sprintf Basics
Format a number with a fixed number of decimal places.
sprintf-basics
`sprintf` returns formatted text without printing immediately.
Sprintf Basics
sprintf_basics.pl
Replay: real traced execution (multi-file project)
use strict;
use warnings;
my $price = 7.5;
my $formatted = sprintf("%.2f", $price);
my $label = "price=$formatted";
print "raw=$price\n";
print "formatted=$formatted\n";
print "label=$label\n";
use strict;
use warnings;
my $price = 3.25;
my $formatted = sprintf("%.2f", $price);
my $label = "price=$formatted";
print "raw=$price\n";
print "formatted=$formatted\n";
print "label=$label\n";
use strict;
use warnings;
my $price = 12;
my $formatted = sprintf("%.2f", $price);
my $label = "price=$formatted";
print "raw=$price\n";
print "formatted=$formatted\n";
print "label=$label\n";
$price ← 7.5, $formatted ← 7.50, $label ← price=7.50
4my $price→ 7.5 = 7.5; #@price=3.25, 125my $formatted→ 7.50 = sprintf("%.2f", $price7.5);6my $label→ price=7.50 = "price=$formatted7.50";78print "raw=$price7.5\n";9print "formatted=$formatted7.50\n";10print "label=$labelprice=7.50\n";outputraw=7.5 formatted=7.50 label=price=7.50
$price ← 3.25, $formatted ← 3.25, $label ← price=3.25
4my $price→ 3.25 = 3.25;5my $formatted→ 3.25 = sprintf("%.2f", $price3.25);6my $label→ price=3.25 = "price=$formatted3.25";78print "raw=$price3.25\n";9print "formatted=$formatted3.25\n";10print "label=$labelprice=3.25\n";outputraw=3.25 formatted=3.25 label=price=3.25
$price ← 12, $formatted ← 12.00, $label ← price=12.00
4my $price→ 12 = 12;5my $formatted→ 12.00 = sprintf("%.2f", $price12);6my $label→ price=12.00 = "price=$formatted12.00";78print "raw=$price12\n";9print "formatted=$formatted12.00\n";10print "label=$labelprice=12.00\n";outputraw=12 formatted=12.00 label=price=12.00