String. format is a new static method in JDK1.5, which has powerful functions. Its main function is to format data, which is roughly divided into these classes (general type, character type, numeric type, date type ). Its syntax is as follows: regular type, character type, and Value Type format specifier Syntax: % [parameter index position $] [conversion identifier] [minimum Guandu] [. reserved precision bits] Conversion Method Date Syntax: % [parameter index location $] [t or T] conversion method these syntax have been described in jdk api in great detail. Here are some examples to help you. Thank you. Java code import java. math. bigDecimal;/*** 1.% [argument_index $] [flags] [width] [. precision] conversion ** String. format ("% 1 $ s", 1234,5678); point to the first parameter to convert to String * String. format ("% 1 $ s % 2 $ s", 1234,5678); format the two parameters as strings and connect them together * String. format ("% s", 1234,5678); point to the first parameter to convert to String * String. format ("% s % 06d", 1234,5678 ); format the first file as "1234" and the second file as "005678 w */public class StringFormat {/*** to process floating point data application scope float, Float, double, Double, and Bi GDecimal * % [argument_index $] [flags] [width] [. precision] conversion * % [index $] [identifier] [minimum width] [Reserved accuracy] conversion Method * identifier: * '-' is left aligned within the minimum width, * '+' cannot be used together with "fill with 0". The result always includes a symbol * ''with a space before the positive value, the result of adding a negative number * '0' before a negative value will be filled with zero * '. 'Each three digits are separated by commas (,) (only applicable to fgG conversion) * '(if the parameter is a negative number, enclose The number in parentheses instead of adding a negative number in the result (only applicable to the eEfgG conversion) * minimum width: the minimum number of digits * Reserved precision: number after the decimal point * Conversion Method: * 'E ', 'E' -- the result is formatted as a decimal number * 'F' in the computer science notation. -- The result is formatted as a common decimal representation * 'G', 'G '-- Based on the actual situation, the system automatically selects the common representation or scientific notation * 'A ', 'A' -- the result is formatted as A hexadecimal floating point number with valid digits and indexes **/public static void formatFloat () {System. out. println (String. format ("% 1 $ e", 1234567890.123456789); // converts to scientific notation to indicate System. out. println (String. format ("% 1 $ 020e", 1234567890.123456789); // converts it to scientific notation, with a length of 20 and fills the System with 0. out. println (String. format ("% 1 $ g", 12345.123456789); // use the converter e or f System to identify the brakes based on the results. out. println (String. format ("% 1 $ a", 12345.1 23456789); // converts it to a hexadecimal floating point number System. out. println (String. format ("% 1 $, f", 1234567890.123456789); // The default decimal places are retained in the conversion result. The three digits are separated by commas (,) and are converted to decimal digits to indicate System. out. println (String. format ("% 1 $, f", 1234567890.123456789); // The default decimal places are retained for the conversion result. The three digits are used to separate System. out. println (String. format ("% 1 $. 10f ", 1234567890.123456789); // The conversion result is to retain the 10-bit precision. convert to the decimal format: System. out. println (String. format ("% 1 $ ,. 100f ", new BigDecimal (" 12345678909.1234567890123456789 "))); // The conversion result is a 100-bit precision retained, with no loss of precision. Use the three digits to separate the System. out. println (String. format ("% 1 $ ,. 5f ", 1234567890.123456789); // The Conversion Result is retained with 5 decimal places. The value is used for three digits, separated}/*** Processing Integer Data Application Range: byte, Byte, short, Short, int, Integer, long, Long, And BigInteger *** % [argument_index $] [flags] [width] conversion * % [index $] [identifier] [minimum width] conversion Method * identifier: * '-' is left aligned within the minimum width. It cannot be used with "fill with 0" at the same time. * '#' is only applicable to octal and hexadecimal formats, add a hexadecimal value before the result in the 8-hexadecimal format and add 0x * '+' before the result. The result always contains a symbol (generally only applies to the 10-hexadecimal format, if the object is BigI Nteger can only be used in octal and hexadecimal) * ''adds spaces before a positive value, and adds a negative number before a negative value (generally, it is only applicable to the decimal system, if the object is BigInteger, it can be used in octal or hexadecimal notation. * '0' results are filled with zeros * '.' is only applicable to decimal notation, separate the three digits with commas (,) * '(' If the parameter is a negative number, instead of adding a negative sign to the result, enclose the number with parentheses (the same as '+') * minimum width: the minimum number of digits in the string to be converted by this integer * Conversion Method: d-decimal o-octal x or X-hexadecimal */public static void formatNumber () {System. out. println (String. format ("% 1 $ d",-31); // format it into a numeric value. The result is "-31" System. out. println (String. format ("% 1 $ o",-31); // format it to a value. Indicates that the octal sequence is used. The result "37777777741" System. out. println (String. format ("% 1 $ 19d",-31); // format it into a numeric value to indicate that it is in decimal format. The total length is 19 digits and the result is "-31" System. out. println (String. format ("% 1 $-19d",-31); // format it into a numeric value to indicate that it is in decimal format. The total length is 19 digits, and the result is rounded to the left. "-31" System. out. println (String. format ("% 1 $ 09d",-31); // format it into a numeric value, in decimal format. The result is "-00000031" System. out. println (String. format ("% 1 $, 9d",-3123); // each 3 digits are separated by commas (,). The total length is 9 digits. The result is "-3,123" System. out. println (String. format ("% 1 $, 09d",-3 123); // each three digits are separated by commas (,). The total length is 9 digits with 0. The result is "-0003,123" System. out. println (String. format ("% 1 $ (9d",-3123); // this parameter is used for every three digits. The total length is 9 digits with 0. The result is "(3123)" System. out. println (String. format ("% 1 $ 9d",-31);}/*** process character data * it is very easy to format characters, c C Represents characters, '-' in the identifier indicates the left alignment, and nothing else */public static void formatChar () {System. out. println (String. format ("% 1 $ c", 97); // convert to the character System. out. println (String. format ("% 1 $ 10c", 'hangzhou'); // converts the string to a character, with ten System characters. out. pri Ntln (String. format ("% 1 $-10c", 'hong'); // convert to character, ten digits, left}/*** format percentage. % special characters. The escape format is % instead of \ */public static void formatBaiFenBi () {System. out. println (String. format ("% 1 $ f %", 12.123456); System. out. println (String. format ("% 1 $. 4f % ", 12.123456); // returns four decimal places, 4 homes 5 into BigDecimal a = new BigDecimal (" 12.12 "), B = new BigDecimal ("13.13"); BigDecimal c =. divide (B, 28, BigDecimal. ROUND_HALF_UP); // retain the 28-digit decimal number System. out. println (c + ""); System. out. println (String. format ("% 1 $. 28f ", c); // The format is reserved for 28 characters Number}/*** get the independent platform line separator */public static void getSeparator () {System. out. println (String. format ("% n"); System. out. println (System. getProperty ("line. separator ");}/*** format the date (available range: long, Long, Calendar, java. util. date) * % [index $] [identifier] [minimum width] Conversion Mode * identifier: * The prefix t or T * of the Date and time Conversion characters. Conversion Method: * format the date conversion character * 'B' is the full name of the month specific to the language environment, for example, "January" and "February ". * 'B' refers to the abbreviated month specific to the language environment, such as "Jan" and "Feb ". * 'H' is the same as 'B. * 'A is the full name of the day of the week specific to the language environment, for example, "Sunday" and "Monday" * 'a is the abbreviation of the day of the week specific to the language environment, for example, "Sun" and "Mon" * 'C' are divided by the four-digit year of 100, which is formatted as two digits with leading zeros if necessary, that is, 00-99 * 'y' year, which is formatted as a four-digit (at least) with a leading zero when necessary, for example, 0092 equals 92 CE of the Gregorian calendar. * The last two digits of the 'y' year are formatted as two digits with leading zeros (00-99) if necessary. * The number of days in a year in 'J' is formatted as a three-digit number with a leading zero when necessary. For example, for a Gregorian calendar, the value is 001-366. * The 'M' month is formatted as a two-digit with a leading zero when necessary, that is, 01-13. * 'D' indicates the number of days in a month, which is formatted as the number of days in a month with the leading zero and double digits (01-31 * 'E') if necessary. The number of days in a month is formatted as two digits, that is, 1-31. ** Format the time character * 'H' in the 24-hour format. If necessary, it is formatted with two digits leading to zero, that is, 00-23. * The hour in the 'I' 12-hour format is formatted as a two-digit number with the leading zero when necessary, that is, 01-12. * The hour in the 'K' 24-hour format, that is, 0-23. * The hour in the 'l' 12-hour format, that is, 1-12. * Minutes in the 'M' hour are formatted as two digits with leading zeros (00-59) if necessary. * Seconds in 's' minutes are formatted as two digits with leading zeros when necessary, that is, 00-60 ("60" is a special value required to support leap seconds ). * The millisecond value in 'l' seconds is formatted as the number of three digits with the leading zero when necessary, that is, 000-999. * In 'n' seconds, it is formatted as a nine-digit number with a leading zero when necessary, that is, 000000000-999999999. * 'P' indicates the language environment in lowercase in the morning or afternoon, for example, "am" or "pm ". You can use the conversion prefix 'T' to forcibly convert the output to an upper-case format. * The offset of 'Z' to the digital time zone in RFC 822 format of GMT, for example,-0800. * 'Z' indicates a string abbreviated to the time zone. The Formatter language environment replaces the parameter language environment (if any ). * The number of seconds between the Coordinated Universal Time (UTC) January 1, 1970 00:00:00 and now, that is, the difference between Long. MIN_VALUE/1000 and Long. MAX_VALUE/1000. * 'Q' indicates the number of milliseconds that have elapsed since 00:00:00, January 1, January 1, 1970, Coordinated Universal Time (UTC), that is, Long. MIN_VALUE and Long. difference between MAX_VALUE * format the time combination character * 'R' in the 24-hour format, formatted as "% tH: % tM" * 't'24-hour time, formatted as "% tH: % tM: % tS ". * The 'r' 12-hour time is formatted as "% tI: % tM: % tS % Tp ". The location marked in the morning or afternoon ('% Tp') may be related to the language environment. * 'D' date, formatted as "% tm/% td/% ty ". * Complete date in 'f' ISO 8601 format, formatted as "% tY-% tm-% td ". * 'C' date and time are formatted as "% ta % tb % td % tT % tZ % tY", for example, "Sun Jul 20 16:17:00 EDT 1969 ". **/Public static void formatDate () {long c = System. currentTimeMillis (); System. out. println (String. format ("% 1 $ tY-% 1 $ tm-% 1 $ td % 1 $ tH: % 1 $ tM: % 1 $ tS", c); System. out. println (String. format ("% 1 $ ty-% 1 $ tm-% 1 $ td % 1 $ tI: % 1 $ tM: % 1 $ tS % 1 $ tp % 1 $ tb % 1 $ ta ", c); System. out. println (String. format ("% 1 $ tF % 1 $ tT", 1244943575031l ));} /* the conversion can be applied to any parameter type * 'B or B' \ u0062 '. "true" or "false" is generated. If the parameter is null, the result is "false ". If the parameter is a boolean value or Boolean, the result is a String returned by String. valueOf. Otherwise, the result is "true ". * 'H or h'' \ u0068 'generates a string that represents the hash code value of the object. If the arg parameter is null, the result is "null ". Otherwise, the result is obtained by calling Integer. toHexString (arg. hashCode. *'S or s' \ u0073 'generates a string. If the parameter is null, the result is "null ". If Formattable is implemented, the formatTo method is called. Otherwise, the result is the result obtained by calling the toString () method of the parameter. * **/Public static void formatAny () {System. out. println (String. format ("% B", null, "", "true", true )); string pattern = "% 1 $ s in % 4 $ tF % 4 $ tT said \" % 1 $ s love % 2 $ s % 3 $ d Year \""; system. out. println (String. format (pattern, "mingming", "shuilian", 10000, System. currentTimeMillis ();} public static void main (String [] args) {formatAny ();}}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.