Perl Topics





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
Perl Tutorial
Introduction Introduction
Installing Perl Installing Perl
Basics of Perl Basics of Perl
Scalar Variables Scalar Variables
Operators Operators
Control Structures Control Structures
Lists Lists
Array Array
Arrays Manipulation Arrays Manipulation
Arrays Functons Array Functions
Hash Hash
Hash Functions Hash Functions
String Functions String Functions
Regular Expression Regular Expression
Regular Expression Functions Regular Expression Functions
Numerical Functions Numerical Functions
List Functions List Functions
User Defined Function User Defined Function
File Handling File Handling
Forums Ask Your Doubts
Feedback Feedback
 





printf Function in Perl


Tutorials Perl

Topic

What is printf Function in Perl?



Explanation

This function is used to have a formatted output from a list.

Syntax:
    printf FILEHANDLE FORMAT, LIST
    printf FORMAT, LIST

The following are the formatting conversions that can be used.

Character Description
%% a percent sign
%c a character with the given number
%s a string
%d a signed integer, in decimal
%u an unsigned integer, in decimal
%o an unsigned integer, in octal
%x an unsigned integer, in hexadecimal
%e a floating-point number, in scientific notation
%f a floating-point number, in fixed decimal notation
%g a floating-point number, in %e or %f notation
%X like %x, but using upper-case letters
%E like %e, but using an upper-case "E"
%G like %g, but with an upper-case "E" (if applicable)
%p a pointer outputs the Perl value's address in hexadecimal
%n the number of characters output so far into the next variable

Perl also supports the following flags to get a formatted output.

Flag Result
space prefix positive number with a space
+ prefix positive number with a plus sign
- left-justify within the field
0 use zeros, not spaces, to right-justify
# ensure the leading "0" for any octal, non-zero hexadecimal with "0x" or "0X",non-zero binary with "0b" or "0B"
Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    printf("Decimal Number is: %d", 234.00);
    print "<br>";
    printf("Floating Point Number: %f",234.55);
    print "<br>";
    printf("Binary value of decimal 2: %b",2);   
    print "<br>";
Result:
    Decimal Number is: 234
    Floating Point Number: 234.550000
    Binary value of decimal 2: 10

In the above example printf Function, the floating point number is printed as a decimal number, in the second example the floating point number is rounded to 6 decimal places, in the third example the binary value of "2" is printed as "10".






A Note
Simple introduction, basic CGI perl programming codes with examples. Do send your feedback or suggestions on this tutorial. This is a copyright content.

Other Links

web hosting