linux 终端使用 printf 命令

 

001、输出字符串

[root@pc1 test1]# ls
[root@pc1 test1]# printf "hello world\n"        ## 方法一
hello world
[root@pc1 test1]# printf "%s\n" "hello world"   ## 方法二
hello world

 

002、输出整型

[root@pc1 test1]# ls
[root@pc1 test1]# printf "100\n"            ## 方法1
100
[root@pc1 test1]# printf "%d\n" 100         ## 方法2
100
[root@pc1 test1]# printf "%5d\n" 100        ## 指定宽度5
  100
[root@pc1 test1]# printf "%05d\n" 100       ## 指定宽度5,同时指定用0占位
00100

 

003、输出浮点型

[root@pc1 test1]# ls
[root@pc1 test1]# printf "10.56\n"              ## 方法1
10.56
[root@pc1 test1]# printf "%f\n" 10.56           ## 方法2
10.560000
[root@pc1 test1]# printf "%.1f\n" 10.56         ## 指定一位小数
10.6
[root@pc1 test1]# printf "%8.1f\n" 10.56        ## 指定宽度为8
    10.6
[root@pc1 test1]# printf "%08.1f\n" 10.56       ## 用0占位
000010.6

 

 004、科学计数法

[root@pc1 test1]# ls
[root@pc1 test1]# printf "%e\n" 110400       ## 科学计数法
1.104000e+05
[root@pc1 test1]# printf "%8.2e\n" 110400     ## 两位小数
1.10e+05
[root@pc1 test1]# printf "%E\n" 110400        ## 同上
1.104000E+05
[root@pc1 test1]# printf "%8.2E\n" 110400     ## 同上
1.10E+05

 。

 

posted @ 2023-12-31 22:05  小鲨鱼2018  阅读(113)  评论(0)    收藏  举报