From: http://codingstandards.iteye.com/blog/1198098
参见:http://download.oracle.com/javase/6/docs/api/java/util/Formatter.html#syntax
static String format(String format, Object... args)
使用指定的格式字符串和参数返回一个格式化字符串。
参见:String.format函数使用方法介绍 http://blog.csdn.net/andycpp/article/details/1749700
参见:http://www.java2s.com/Code/JavaAPI/java.lang/System.out.printf.htm
1. | System.out.printf('%b', String str ) |
2. | System.out.printf('%c', char ch ) |
3. | System.out.printf('%03d', int i ) |
4. | System.out.printf('%e', float ) |
5. | System.out.printf('%03f', float f) |
6. | System.out.printf('%.2f', float f ) |
7. | System.out.printf('{%07.3f}', float f ) |
8. | System.out.printf('%f', float f ) |
9. | System.out.printf('%g', float f ) |
10. | System.out.printf('%h', float f ) |
11. | System.out.printf('%s', 5) |
12. | System.out.printf('%s://%s/%s\n', String str1, String str2, String str3) |
13. | System.out.printf('%1 s...', String str ) |
14. | System.out.printf('%5s', String str) |
15. | System.out.printf('%-5s', String str) (2) |
16. | System.out.printf( '%-10.10s %s', String word, int length ) |
17. | System.out.printf('%.5s', String str) (3) |
18. | System.out.printf('%s', Date date ) |
19. | System.out.printf('%tc', Date date ) (lowercase t, lowercase c) |
20. | System.out.printf('%tC', Date date ) (lowercase t, uppercase C) |
21. | System.out.printf('%tD', Date date ) |
22. | System.out.printf('%tF', Date date ) |
23. | System.out.printf('%tr', Date date ) |
24. | System.out.printf('%tR',Date date ) |
25. | System.out.printf('%tT', Date date ) |
26. | System.out.printf('%tz', Date date ) |
27. | System.out.printf('%Tc', Date date ) (Uppercase T, lowercase c) |
28. | System.out.printf('%1x, %1X', 0xCAFE ) |
29. | System.out.printf( Locale.CHINA, '%tc', Date date ) |
30. | System.out.printf( Locale.ITALIAN, '%tc', Date date ) |
如果你想对printf命令更深入的了解,参见 http://wiki.bash-hackers.org/commands/builtin/printf
示例来自 http://ss64.com/bash/printf.html
# Use \n to start a new line
$ printf "Two separate\nlines\n"
Two separate
lines
[root@jfht ~]# printf "Two separate\nlines\n"
Two separate
lines
[root@jfht ~]# echo "Two separate\nlines\n"
Two separate\nlines\n
[root@jfht ~]# echo -e "Two separate\nlines\n"
Two separate
lines
[root@jfht ~]# echo -n -e "Two separate\nlines\n"
Two separate
lines
[root@jfht ~]#
技巧来自 Zero Padding in Bash http://jonathanwagner.net/2007/04/zero-padding-in-bash/
创建从1到31为名的目录
for ((x=1;x<=31;x+=1)); do mkdir $x; done
一位数字前面加0
for ((x=1;x< =31;x+=1)); do mkdir `printf "%02d" $x`; done
例子来自 http://ss64.com/bash/printf.html
# Echo a list of numbers from 1 to 100, adding 3 digits of Zero padding
# so they appear as 001, 002, 003 etc:
$ for ((num=1;num<=100;num+=1)); do echo `printf "%03d" $num`; done
示例来自 http://linuxconfig.org/bash-printf-syntax-basics-with-examples
#!/bin/bash divider=============================== divider=$divider$divider header="\n %-10s %8s %10s %11s\n" format=" %-10s %08d %10s %11.2f\n" width=43 printf "$header" "ITEM NAME" "ITEM ID" "COLOR" "PRICE" printf "%$width.${width}s\n" "$divider" printf "$format" \ Triangle 13 red 20 \ Oval 204449 "dark blue" 65.656 \ Square 3145 orange .7
[zcm@bash #86]$./a.sh ITEM NAME ITEM ID COLOR PRICE =========================================== Triangle 00000013 red 20.00 Oval 00204449 dark blue 65.66 Square 00003145 orange 0.70 [zcm@bash #87]$