Pages

Tuesday, August 6, 2013

Formatting Numbers in Perl

Introduction to Formatting Numbers

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 are 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

Format

Result

A percent sign

%%

%

Convert the integer to character

%c

_â++++¦

Convert the integer to a string

%s

-10.349

Display as a signed integer

%d

-10

Display as unsigned decimal integer

%u

4294967286

Display as an unsigned octal integer

%o

37777777766

Display as an unsigned hexadecimal integer

%x

fffffff6

Same as above but letters are displayed in caps

%X

FFFFFFF6

Display as a scientific floating point number

%e

-1.034900e+001

Same as above but letters are displayed in caps

%E

-1.034900E+001

Display number with decimal point

%f

-10.349000

Display the integer either in %e or %f format

%g

-10.349

Same as above but cletters are displayed in caps

%G

-10.349

Display in binary

%b

11111111111111111111111111110110

Display the value's address in hexadecimal

%p

1765764

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:

Description

Flag

Format

Result

Length of the formatted number

any number

%10f

-10.349000

Add space to a positive number

space

% f

-10.349000

Add a positive sign to a positive number

+

%+f

-10.349000

Left justify

-

%-f

-10.349000

pad zeroes to right justify

0

%0f

-10

specify number of decimal digits

.number

%.2f

-10.35

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.

-10.35

If you want to pad this number with zeroes, you can do this:

sprintf("%010.2f", $a);

The result will be:

-000010.35

For more information on the sprintf and printf functions, read the perlfunc documentation