0% found this document useful (0 votes)
106 views2 pages

Converters and Flags Used in TestFormat

The document describes various conversion specifiers and flags used in the TestFormat.java code to format numeric, date, and time output. Some key specifiers are: - %d for integers - %f for floating point numbers - %t for date/time values like month name, hour, minute, etc. - Flags like + and , control sign and grouping separators. The TestFormat code demonstrates formatting integers, floating point values, and date/time output using these specifiers and flags.

Uploaded by

Rohit Khanduri
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views2 pages

Converters and Flags Used in TestFormat

The document describes various conversion specifiers and flags used in the TestFormat.java code to format numeric, date, and time output. Some key specifiers are: - %d for integers - %f for floating point numbers - %t for date/time values like month name, hour, minute, etc. - Flags like + and , control sign and grouping separators. The TestFormat code demonstrates formatting integers, floating point values, and date/time output using these specifiers and flags.

Uploaded by

Rohit Khanduri
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Converters and Flags Used in TestFormat.

java
Converter Flag Explanation
d   A decimal integer.
f   A float.
A new line character appropriate to the
n   platform running the application. You should
always use %n, rather than \n.
A date & time conversion—locale-specific
tB  
full name of month.
A date & time conversion—2-digit day of
td, te   month. td has leading zeroes as needed, te
does not.
A date & time conversion—ty = 2-digit year,
ty, tY  
tY = 4-digit year.
A date & time conversion—hour in 12-hour
tl  
clock.
A date & time conversion—minutes in 2
tM  
digits, with leading zeroes as necessary.
A date & time conversion—locale-specific
tp  
am/pm (lower case).
A date & time conversion—months in 2
tm  
digits, with leading zeroes as necessary.
A date & time conversion—date as %tm%td
tD  
%ty
Eight characters in width, with leading
  08
zeroes as necessary.
  + Includes sign, whether positive or negative.
  , Includes locale-specific grouping characters.
  - Left-justified..
  .3 Three places after decimal point.
Ten characters in width, right justified, with
  10.3
three places after decimal point.

import java.util.Calendar;
import java.util.Locale;
public class TestFormat {

public static void main(String[] args) {


long n = 461012;
System.out.format("%d%n", n); // --> "461012"
System.out.format("%08d%n", n); // --> "00461012"
System.out.format("%+8d%n", n); // --> " +461012"
System.out.format("%,8d%n", n); // --> " 461,012"
System.out.format("%+,8d%n%n", n); // --> "+461,012"

double pi = Math.PI;
System.out.format("%f%n", pi); // --> "3.141593"
System.out.format("%.3f%n", pi); // --> "3.142"
System.out.format("%10.3f%n", pi); // --> " 3.142"
System.out.format("%-10.3f%n", pi); // --> "3.142"
System.out.format(Locale.FRANCE,
"%-10.4f%n%n", pi); // --> "3,1416"

Calendar c = Calendar.getInstance();
System.out.format("%tB %te, %tY%n", c, c, c); // --> "May 29, 2006"
System.out.format("%tl:%tM %tp%n", c, c, c); // --> "2:34 am"
System.out.format("%tD%n", c); // --> "05/29/06"
}
}

You might also like