string.format Usage: 1. Use the local language environment for the new string, formulate the string format and parameters to generate a formatted new string; 2. Use the specified language environment, formulate the string format and parameter generation Formatted string.
String str=null; str=String.format("Hi,%s", "小超"); System.out.println(str); str=String.format("Hi,%s %s %s", "小超","是个","大帅哥"); System.out.println(str); System.out.printf("字母c的大写是:%c %n", 'C'); System.out.printf("布尔结果是:%b %n", "小超".equal("帅哥")); System.out.printf("100的一半是:%d %n", 100/2); System.out.printf("100的16进制数是:%x %n", 100); System.out.printf("100的8进制数是:%o %n", 100); System.out.printf("50元的书打8.5折扣是:%f 元%n", 50*0.85); System.out.printf("上面价格的16进制数是:%a %n", 50*0.85); System.out.printf("上面价格的指数表示:%e %n", 50*0.85); System.out.printf("上面价格的指数和浮点数结果的长度较短的是:%g %n", 50*0.85); System.out.printf("上面的折扣是%d%% %n", 85); System.out.printf("字母A的散列码是:%h %n", 'A');
Hi,小超 Hi,小超 是个 大帅哥 字母c的大写是:C 布尔的结果是:false 100的一半是:50 100的16进制数是:64 100的8进制数是:144 50元的书打8.5折扣是:42.500000 元 上面价格的16进制数是:0x1.54p5 上面价格的指数表示:4.250000e+01 上面价格的指数和浮点数结果的长度较短的是:42.5000 上面的折扣是85% 字母A的散列码是:41
Conversion symbols | Detailed description | Example |
---|---|---|
String type | "Like Please bookmark" | |
Character type | 'm' | |
Boolean type | true | ##%d |
88 | %x | |
FF | ##%o | |
77 | ##%f | Floating point type |
% a | Hexadecimal floating point type | |
##%e | Exponential type | |
%g | General floating point type (the shorter of f and e types) | |
%h | Hash code | |
%% | Percent type | |
##%n | Line break | No examples (basically not used) |
%tx | Date and time types (x represents different date and time conversion characters) | No examples (basically not used) |
For the convenience of understanding, let’s give an example | Output results |
Result | |||
---|---|---|---|
15 | 0 | Add 0 in front of the number (commonly used for encryption) | |
0099 | Spaces | Adds the specified number of spaces before the integer | |
99 | ##, | Use "," to group numbers (commonly used to display the amount) | ("%,f", 9999.99) |
( | Use brackets to include negative numbers | (“%(f”, -99.99) | |
#If it is a floating point number, include the decimal point, if it is hexadecimal or octal, add 0x or 0 | (“%#x”, 99)(“%#o”, 99) | ||
##< | Format the previous one The parameters described by the conversion operator | ("%f and %<3.2f", 99.45) | |
d,% 2$s”, 99,”abc”) | 99,abc | ||
First one In the example, it is mentioned that %tx |
Includes full date and time information
F | "Year-Month-Day" format | 2007-10-27 |
---|---|---|
"Month/Day/Year" format | 10/27/07 | |
"HH:MM:SS PM" format ( 12-hour format) | 02:25:51 PM | |
"HH:MM:SS" format (24-hour format) | 14:28:16 | |
"HH:MM" format (24-hour format) | 14:28 | |
Let’s give an example to facilitate understanding |
Date date=new Date(); //c的使用 System.out.printf("全部日期和时间信息:%tc%n",date); //f的使用 System.out.printf("年-月-日格式:%tF%n",date); //d的使用 System.out.printf("月/日/年格式:%tD%n",date); //r的使用 System.out.printf("HH:MM:SS PM格式(12时制):%tr%n",date); //t的使用 System.out.printf("HH:MM:SS格式(24时制):%tT%n",date); //R的使用 System.out.printf("HH:MM格式(24时制):%tR",date);
Copy after login
| Output results
全部日期和时间信息:星期三 九月 21 22:43:36 CST 2016 年-月-日格式:2016-09-21月/日/年格式:16/10/21 HH:MM:SS PM格式(12时制):10:43:36 下午 HH:MM:SS格式(24时制):22:43:36 HH:MM格式(24时制):22:43
Copy after login
|
java basic tutorial |
The above is the detailed content of What is the usage of String.format?. For more information, please follow other related articles on the PHP Chinese website!