Java string Formatting--java.util.formatter

Source: Internet
Author: User
Tags local time locale

Java.util.Formatter is a new class of JDK1.5 that supports printf-style string formatting works like C


Formatter has 4 construction methods, as follows:

Public Formatter ()

Public Formatter (Appendable a)

Public Formatter (Locale l)

Public Formatter (Appendable A, Locale l)

The construction method is primarily used to set the buffer and locale of the formatter, by default,formatter uses StringBuilder as the buffer and Locale.getdefault () as the locale.


Formatter is used as follows (from JDK DOC):

 stringbuilder sb = new stringbuilder (); // send all output  To the appendable object sb formatter formatter = new formatter ( sb, locale.us); // explicit argument indices may be used to  Re-order output. formatter.format ("%4$2s %3$2s %2$2s %1$2s",  "a",  "B",   "C",  "D")  // ->  " d  c  b  a"  //  Optional locale as the first argument can be used to get  // locale-specific formatting of numbers.  the precision and  width can be // given to round and align the value.  formatter.format (locale.france,  "e = %+10.4f",  MATH.E); // ->  "E  =    +2,7183 " // The " ('  numeric flag may be used to format  Negative numbers with // parentheses rather than a minus sign.   group separators are // automatically inserted. formatter.format (" amount gained or lost since last statement: $ % (,. 2f ",  Balancedelta); // ->  "amount gained or lost since last  statement: $  (6,217.58) "

Example, a new formatter object is first called, and then the formatter's format method is invoked, and the buffer inside the formatter stores the formatted string, which can then be removed using the ToString method.


A more general usage is to use String.format,string.format to implement the note string format via formatter

public static string format (string format, Object ... args) {return new Formatter (). Format (format, args). ToString ();}


or use it in the output stream.

System.out.format ("Local Time:%tt", calendar.getinstance ());


The main parameters of the format method are divided into two parts, a format string, and a mutable format parameter


A format string has its own syntax, which consists of a fixed text or a format descriptor. The format of the descriptor is as follows:

%[argument_index$][flags][width][.precision]conversion

argument_index is an integer that indicates the position of the parameter used, for example

System.out.format ("%d%d", 1, 2);      Output 1 2system.out.format ("%1$d%d", 1, 2); Output 1 1

flags is a set of notes used to decorate the output

width is a non-negative integer that specifies the minimum length to write to Suizhong

Precision is a nonnegative integer that is typically used to specify the precision of a number, depending on the conversion

Conversion must have a value to specify how the argument is converted


Conversion There are several groups of characters (that is, types that can be converted)

' s ', ' s ' requires the parameter to implement the Formattable, and then calls its Formatto method to get the result

' C ', ' C ' character

' d ' decimal integer

' o ' octal integer

' X ', ' x ' hexadecimal integer

' E ', ' e ' scientific notation floating point number

' F ' floating-point number

' t ', ' t ' time date


Flags There are several ways to use the main

'-' Align Left

' # ' depends on conversion, different conversion, the flag does not have the same meaning

' + ' results with plus sign

"Result with front space

' 0 ' If the result does not meet The requirements of the width parameter , fill with 0 characters

', ' the number will be separated by a comma by locale

' (' Negative results are enclosed in parentheses


More detailed information can be seen in the JDK documentation

Http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html

Java string Formatting--java.util.formatter

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.