String.format() is a regular type of string format(String format, Object… args) new strings use the local locale, formulate the string format and parameters to generate formatted new strings. format(Locale locale, String format, Object… args) uses the specified locale to formulate string formats and parameters to generate formatted strings.
| Converter | Detailed description | example | | %s | String type | "Hello buddy." | | %c | Character type | ‘m’ | | %b | Boolean type | true | | %d | Integer type | (decimal) 88 | | %x | Integer type | (hexadecimal) FF | | %o | Integer type | (Octal) 77 | | %f | Floating-point type | 8.888 | | %a | Hex floating-point type | FF.35AE | | %e | Index type | 9.38e+5 | | %g | Universal floating-point type (shorter of the f and e types) | No examples (basically not used) | | %h | hash code | No examples (basically not used) | | %% | Percentage type | %(%Special characters%% to display) | | %n | Line break | No examples (basically not used) | | %tx | Date and Time Type (x for different Date and Time Converts) | No examples (basically not used) |
example
with converters
| sign | illustrate | example | outcome | | + | Add symbols to positive or negative numbers | (“%+d”,15) | +15 | | 0 | Prefix the number with 0 (commonly used for encryption) | (“%04d”, 99) | 0099 | | space | Add a specified number of spaces before the integer | (“% 4d”, 99) | 99 | | , | Group numbers by "," (commonly displayed amounts) | (“%,f”, 9999.99) | 9,999.990000 | | ( | Use parentheses to contain negative numbers | (“%(f”, -99.99) | (99.990000) | | # | If it is a floating-point number, it contains a decimal point, and if it is a hexadecimal or 8-decimal number, it adds 0x or 0 | (“%#x”, 99) (“%#o”, 99) | 0x63 0143 | | < | Format the parameters described by the previous converter | (%f and %< 3.2f", 99.45) | 99.450000 and 99.45 | | d,%2$s”, 99,”abc”) | 99,abc | | |
Formatted output of dates
| sign | illustrate | example | | c | Include full date and time information | Sat Oct 27 14:21:20 CST 2007 | | F | "YYYYYYYYYYYYYYYYYYYYYYYYYYY | 2007-10-27 | | D | Month/Day/Year format | 10/27/07 | | r | "HH:MM:SS PM" format (12 hours) | 02:25:51 pm | | T | "HH:MM:SS" format (24-hour format) | 14:28:16 | | R | "HH:MM" format (24-hour system) | 14:28 |
example
Original:The hyperlink login is visible. |