Saturday, June 27, 2009

Perl: Left padding an integer using sprintf

Share at Facebook

Using the function sprintf() of Perl, you can easily left pad any given integer number easily. The sprintf() function is similar to use like other programming languages. I'm giving an example of showing a given integer number with 5 field, where left sides will filled with zeros if any.

my $integer = 10;
$integer = sprintf("%05d", $integer);
print "$integer\n";

Just a simple left padding.

No comments: