Is there an equivalent method to C's scanf in Java?

前端 未结 7 1179
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 11:58

Java has the notion of format strings, bearing a strong resemblance to format strings in other languages. It is used in JDK methods like String#format() for output conversio

7条回答
  •  情歌与酒
    2020-12-01 12:43

    You can format your output in Java as described in below code snippet.

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

    You can read more here.

提交回复
热议问题