how do I use System.out.printf?

前端 未结 1 1863
被撕碎了的回忆
被撕碎了的回忆 2020-12-11 08:46

My teacher wants us to display our values in the format method (at the very bottom) but the problem is we had a sub and she didn\'t show us how to use it and my teacher is b

相关标签:
1条回答
  • 2020-12-11 09:35

    Here a few examples:

    (The output is shown within double quotes in the embedded comment)

    Note : System.out.format() = System.out.printf()

    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"
        }
    }
    

    (source)

    0 讨论(0)
提交回复
热议问题