perl中sprintf函数的使用方法

对于某些字符串,需要输入为特定的格式,通过sprintf可以很方便的完成,不需要专门进行其他处理。

perl中的sprintf的用法如下:

字符串变成相应的数字eval

sprintf FORMAT, LIST

 $num1=0x498;

$num2  = sprintf("%d",$num1);

print "the num2 zero value is $num2\n";


$num2  = $num2>>2;

print "the num2 second value is $num2\n";


$num2 = sprintf("%x",$num2);

print "the num2 third value is $num2\n";


$num0=0x4e8>>2;

print "the num0 value is $num0\n";


print "Generate regfile.v successfully!\n";


#$BaseAddress = "efH";

#$Offset      = "3eH";

#$BaseAddress =~ s/H//; 

#$Offset      =~ s/H//; 

#$addr_init = "0x$BaseAddress$Offset";

$addr_init = "0xefe";

$addr_ch   = sprintf ("%x",eval_r($addr_init)>>2);

print "Generate $addr_ch successfully!\n";

比如:

$result = sprintf("d",$number);让$number有8个前导零。

 

$rounded = sprintf("%.3f",$number);

让小数点后有3位数字。

 

sprintf允许的如下常用的转换:

 

%%         百分号

%c           把给定的数字转化为字符

%s           字符串

%d           带符号整数,十进制

%u           无符号整数,十进制

%o           无符号整数,八进制

%x           无符号整数,十六进制

%e           浮点数,科学计算法

%f            浮点数,用于固定十进制计数

%g           浮点数,包括%e和%f

 

 %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)
 %b   an unsigned integer, in binary
 %B   like %b, but using an upper-case "B" with the # flag
 %p   a pointer (outputs the Perl value's address in hexadecimal)
 %n   special: *stores* the number of characters output so far
      into the next variable in the parameter list

 

通过$1,$2等可以改变顺序:

 printf '%2$d %1$d', 12, 34;      # prints "34 12"
 printf '%3$d %d %1$d', 1, 2, 3;  # prints "3 1 1"

 


printf '<% d>',  12;   # prints "< 12>"
printf '<%+d>',  12;   # prints "<+12>"
printf '<%6s>',  12;   # prints "<    12>"
printf '<%-6s>', 12;   # prints "<12    >"
printf '<s>', 12;   # prints "<000012>"
printf '<%#o>',  12;   # prints "<014>"
printf '<%#x>',  12;   # prints "<0xc>"
printf '<%#X>',  12;   # prints "<0XC>"
printf '<%#b>',  12;   # prints "<0b1100>"
printf '<%#B>',  12;   # prints "<0B1100>"

 

 printf '<%f>', 1;    # prints "<1.000000>"
 printf '<%.1f>', 1;  # prints "<1.0>"
 printf '<%.0f>', 1;  # prints "<1>"
 printf '<%e>', 10;   # prints "<1.000000e+01>"
 printf '<%.1e>', 10; # prints "<1.0e+01>"

 

printf '<%.6d>', 1;      # prints "<000001>"
printf '<%+.6d>', 1;     # prints "<+000001>"
printf '<%-10.6d>', 1;   # prints "<000001    >"
printf '<.6d>', 1;    # prints "<    000001>"
printf '<0.6d>', 1;   # prints "<    000001>"
printf '<%+10.6d>', 1;   # prints "<   +000001>"

printf '<%.6x>', 1;      # prints "<000001>"
printf '<%#.6x>', 1;     # prints "<0x000001>"
printf '<%-10.6x>', 1;   # prints "<000001    >"
printf '<.6x>', 1;    # prints "<    000001>"
printf '<0.6x>', 1;   # prints "<    000001>"
printf '<%#10.6x>', 1;   # prints "<  0x000001>"

 

printf '<%.5s>', "truncated";   # prints "<trunc>"
printf '<.5s>', "truncated"; # prints "<     trunc>"

 

             printf "%2/$d %d/n",    12, 34;               # will print "34
"
             printf "%2/$d %d %d/n", 12, 34;               # will print "34
4/n"
             printf "%3/$d %d %d/n", 12, 34, 56;           # will print "56
4/n"
             printf "%2/$*3/$d %d/n", 12, 34, 3;           # will print " 3
n"

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值