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);

}

}

, ,

  1. #1 by CNA Training at June 22nd, 2010

    Valuable info. Lucky me I found your site by accident, I bookmarked it.

  2. #2 by cna training at June 29th, 2010

    Wow this is a great resource.. I’m enjoying it.. good article

  3. #3 by CASTILLOIRMA at August 7th, 2010

    I always encourage university students to buy essay papers because it’s great solution for people who are lack of time.

(will not be published)
  1. No trackbacks yet.