perl function sprintf

www‮gi.‬iftidea.com

The sprintf function in Perl is used to format a string based on a specified format.

Here's an example of using the sprintf function:

my $number = 42.75;
my $formatted_number = sprintf("%.2f", $number);

print $formatted_number;

Output:

42.75

In this example, we have a number $number with a decimal point. We use the sprintf function to format the number with two decimal places using the format string "%.2f". The "%" character indicates that we want to format a variable, and the ".2" specifies that we want two decimal places. The "f" character specifies that we want to format a floating-point number.

We then assign the formatted string to $formatted_number and print it using the print function.