Demonstrates the PRECISION modifier in Formatter Class.

A Precision modifier can be applied to any format specifier. It follows the minimum field width specifier(if it is there) and consists of a period followed by an integer.  The default precision is 6. For example, %10.4f  prints a number at least ten character wide with four decimal places. In case of String, it specifies the maximum field width. For example, %5.7s prints a string at least  five and not exceeding  seven characters long.

import java.util.*;

class FormatterExample{

public static void main(String a[]){

Formatter fm= new Formatter();

//Format 4 decimal places

fm.format(" %.4f",123.1234567);

System.out.println(fm);

//Format to 2 decimal placesin a 16 cheracter field

fm= new Formatter();

fm.format(" %16.2e",123.1234567);

System.out.println(fm);

//print at most 15 characters  in a string

fm= new Formatter();

fm.format(" %.15s","I love java.");

System.out.println(fm);

}

}
Advertisements

2 thoughts on “Demonstrates the PRECISION modifier in Formatter Class.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s