Perl printf Function

10 篇文章 0 订阅

Description

This function prints the value of LIST interpreted via the format specified by FORMAT to the current output filehandle, or to the one specified by FILEHANDLE.

Effectively equivalent to print FILEHANDLE sprintf(FORMAT, LIST)

You can use print in place of printf if you do not require a specific output format. Following is the list of accepted formatting conversions.

Sr.No.Format & Result
1

%%

A percent sign

2

%c

A character with the given ASCII code

3

%s

A string

4

%d

A signed integer (decimal)

5

%u

An unsigned integer (decimal)

6

%o

An unsigned integer (octal)

7

%x

An unsigned integer (hexadecimal)

8

%X

An unsigned integer (hexadecimal using uppercase characters)

9

%e

A floating point number (scientific notation)

10

%E

A floating point number, uses E instead of e

11

%f

A floating point number (fixed decimal notation)

12

%g

A floating point number (%e or %f notation according to value size)

13

%G

A floating point number (as %g, but using .E. in place of .e. when appropriate)

14

%p

A pointer (prints the memory address of the value in hexadecimal)

15

%n

Stores the number of characters output so far into the next variable in the parameter list

Perl also supports flags that optionally adjust the output format. These are specified between the % and conversion letter. They are shown in the following table −

Sr.No.Flag & Result
1

space

Prefix positive number with a space

2

+

Prefix positive number with a plus sign

3

-

Left-justify within field

4

0

Use zeros, not spaces, to right-justify

5

#

Prefix non-zero octal with .0. and hexadecimal with .0x.

6

number

Minimum field width

7

.number

Specify precision (number of digits after decimal point) for floating point numbers

8

l

Interpret integer as C-type .long. or .unsigned long.

9

h

Interpret integer as C-type .short. or .unsigned short.

10

V

Interpret integer as Perl.s standard integer type

11

v

Interpret the string as a series of integers and output as numbers separated by periods or by an arbitrary string extracted from the argument when the flag is preceded by *.

Syntax

Following is the simple syntax for this function −

printf FILEHANDLE FORMAT, LIST

printf FORMAT, LIST

Return Value

This function

Example

Following is the example code showing its basic usage −

Live Demo
#!/usr/bin/perl -w
printf "%d\n", 3.1415126;
printf "The cost is \$%6.2f\n",499;
printf "Perl's version is v%vd\n",%^V;
printf "%04d\n", 20;

When above code is executed, it produces the following result −

3
The cost is $499.00
Perl's version is v
0020
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值