Home > Perl > Introduction > Formatting Number in Perl
Formatting Number in Perl
Written by Philip L Yuson   
Who is this for
This article is for those who want to learn how to format numbers in Perl

What you have to know
You need to know basic Perl programming

Let us say that your program is calculating a number in decimal and you needed to print out leading zeros to it, how would you do it?
If there is no need for formatting, you can just use the print command.
my $a = 10.003403;
print "$a";
This gives you the value of $a. But let us say that you need to limit the decimal point to 2 decimal digits, how would you do that?

In Perl, you can use the printf function. You can also use sprintf to set to result to a variable. The printf function allows you to format a number the way that you would want to.

Say in the example, this is a price of an item. You can't display the cost of an item as $10.003403. You need to truncate the number to two decimal places.

When I was beginning to write in Perl, I did not know this function so what I did was to split the number then substring the decimal numbers to 2 and then concatenate them.:
my @val = split('.', $a);
$a = "$val[0]." . substr($val[1], 0, 2);
This did the job but quite bulky and inefficient. Little did I know that Perl has a function to simplify this:
$a = sprintf("%0.2f", $a);
With just one statement, we were able to format the variable to what we wanted.

sprintf Conversion Format
If we have
$a = -10.3490;
and the sprintf format is as specified in the table, the result will be as shown in the table.

The sprintf format can be used to convert an integer to:
Conversion Type
A percent sign
Convert the integer to character
Convert the integer to a string
Display as a signed integer
Display as unsigned decimal integer
Display as an unsigned octal integer
Display as an unsigned hexadecimal integer
Same as above but letters are displayed in caps
Display as a scientific floating point number
Same as above but letters are displayed in caps
Display number with decimal point
Display the integer either in %e or %f format
Same as above but cletters are displayed in caps
Display in binary
Display the value's address in hexadecimal

The above conversions still do not allow us to format the integer. To show an example, we will use these flags with the %d conversion format. To format the integer, you need to specify these flags:

Format Result
Length of the formatted number
any number
Add space to a positive number
% f
Add a positive sign to a positive number
Left justify
pad zeroes to right justify
specify number of decimal digits

You can also mix these flags together. So if you want to have two decimal places and limit the length of the number to 10 digits (including the decimal numbers), you can do this:
    sprintf("%10.2f", $a);
The result will be a number with two decimal places padded with spaces.
If you want to pad this number with zeroes, you can do this:
    sprintf("%010.2f", $a);
The result will be:

For more information on the sprintf and printf functions, read the perlfunc documentation
Copyright: © 2018 Philip Yuson