This article is a mirror article of machine translation, please click here to jump to the original article.

View: 6066|Reply: 0

[Source] Formatting the String format() method in Java

[Copy link]
Posted on 12/16/2021 10:39:41 AM | | |
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.

ConverterDetailed descriptionexample
%sString type"Hello buddy."
%cCharacter type‘m’
%bBoolean typetrue
%dInteger type(decimal) 88
%xInteger type(hexadecimal) FF
%oInteger type(Octal) 77
%fFloating-point type8.888
%aHex floating-point typeFF.35AE
%eIndex type9.38e+5
%gUniversal floating-point type (shorter of the f and e types)No examples (basically not used)
%hhash codeNo examples (basically not used)
%%Percentage type%(%Special characters%% to display)
%nLine breakNo examples (basically not used)
%txDate and Time Type (x for different Date and Time Converts)No examples (basically not used)

example


with converters

signillustrateexampleoutcome
+Add symbols to positive or negative numbers(“%+d”,15)+15
0Prefix the number with 0 (commonly used for encryption)(“%04d”, 99)0099
spaceAdd 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

signillustrateexample
cInclude full date and time informationSat Oct 27 14:21:20 CST 2007
F"YYYYYYYYYYYYYYYYYYYYYYYYYYY2007-10-27
DMonth/Day/Year format10/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.




Previous:JS intercepts the response information for all requests for XMLHttpRequest
Next:Those pits of private SaaS software deployment
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com