Monday, 22 February 2016

Java Tutorial : Java format and printf methods(Converters and flags)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=4lyH3QaOVgA&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java format and printf methods(Converters and flags) 
Java Tutorial : Java format and printf methods(Converters and flags) 
FormatNumericExample.java
import java.util.Calendar;
import java.util.Locale;

public class FormatNumericExample
{

    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"

    }
}
Output
461012
00461012
 +461012
 461,012
+461,012

3.141593
3.142
     3.142
3.142     
3,1416    

February 22, 2016
12:08 pm
02/22/16
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/FormatDemo_format_Converters_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/FormatDemo_format_Converters_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/d0559102086f9677d57877fbba43556aca45fc19/BasicJava/FormatDemo_format_Converters_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge