Format Codes for the sprintf and printf Functions : printf « String « Perl






Format Codes for the sprintf and printf Functions

   

Flag                Description                                  Example

Blank (default)     Positive numbers begin with a blank.         printf FH "% 04.2\n", $D;
                    Negative numbers begin with a minus sign. 
                    Align characters to right. 
                    Pad from left with blanks.                   
                    
                      
-                   Align characters to left.                    $V = sprintf "%-4.2",$D;
                    Pad from right with blanks.    

+                   Align characters to right.                   $V = sprintf "%+4.2", $D;
                    Pad from left with blanks.    

c                   Character format

d                   Decimal format

e                   Exponential format

f                   Floating-point format

g                   Compact format

ld                  Long decimal format

lo                  Long octal format

lu                  Long unsigned decimal format

lx                  Long hexadecimal format

o                   Octal format

s                   String format

u                   Unsigned decimal format

x                   Hexadecimal format

X                   Uppercase hexadecimal format

   
    
    
  








Related examples in the same category

1.Field specifiers for printf.
2.printf Data Formats
3.%.2f specifies a floating-point number with two decimal digits
4.%3d specifies that the integer number should be displayed with three digits
5.%x: Hexadecimal
6.Insert multiple values to printf
7.Output month name and week name by using the return value from gmtime
8.Printing a percent sign after a digit
9.Printing a space before signed values not preceded by + or -
10.Printing integers right-justified
11.Printing numbers with the + flag
12.Printing numbers without the + flag
13.Printing out an array of string one by one
14.Printing with the 0 (zero) flag fills in leading zeros
15.The printf formats a string and prints it to the given file handle:
16.To print a single text string using %s
17.Using precision while printing floating-point numbers
18.Using precision while printing integers
19.Using precision while printing strings
20.Using the # flag with conversion specifier X
21.Using the # flag with conversion specifier g
22.Using the # flag with conversion specifier o
23.printf "%+.4e\n", $value;
24.printf "%.5f\n", $value;
25.printf "%c is ASCII value 65 and %c is value 66\n", 65, 66;
26.printf "%d\n", +455.34;
27.printf "%d\n", -455;
28.printf "%d\n", 455.954;
29.printf "%o\n", 455;
30.printf "%u\n", -455;
31.printf "%u\n", 455;
32.printf "%x\n", -455;
33.printf "Left-justified the number is |%-10d|\n", 100;
34.printf "The character is %c\n", 65;
35.printf "The floating point number is |%8f|\n", 15;
36.printf "The formatted floating point number is |%8.2f|\n",14.3456;
37.printf "The formatted number is |%10d|\n", 100;
38.printf "The number in decimal is %d\n", 45;
39.printf "The number in hexadecimal is %x\n", 15;
40.printf "The number in octal is %o\n",15;
41.printf "The number printed with leading zeros is |%010d|\n", 5;
42.printf "This string is %s\n", "literal";
43.printf("%-15s%-20s\n", "Jack", "Sprat");