zoukankan      html  css  js  c++  java
  • String的format方法--应用

    String的format方法String.format(String format, Object ... args)很有用,String format参数是这样的:%[argument_index$][flags][width][.precision]conversion

    格式化日期

      String.format("%tY", new Date())    //2011
      String.format("%tm", new Date())   //03
      String.format("%tF", new Date())    //2011-03-04
      String.format("%tR", new Date())   //15:49
      String.format("%tT", new Date())   //15:49:34
      String.format("%tc", new Date())   //星期五 三月 04 15:49:34 CST 2011
      String.format("%tD", new Date())  //03/04/11
      String.format("%td", new Date())   //04

    //格式化日期06-07-2016 -可以换成/,%1$tm等的位置也可以交换

    public static void main(String[] args) {
      String s = String.format("%1$tm-%1$td-%1$tY", new Date()); 
      
    System.out.println(s);
    }

    占位符%1$s

        public static void main(String[] args) {
            String s = String.format("这位是%2$s,那位是%1$s", "小明","小黄");
            System.out.println(s);
        }

    格式化数字

    //格式化数字
        public static void main(String[] args) {
            //%[argument_index$][flags][width][.precision]conversion
            //%[argument_index$] : %1$
            //flag:, 金额千分位隔开
            //conversion : d 代表十进制展示
            String s = String.format("%1$,d", 12302562);
            System.out.println(s);
        }

    小数点后保留两位

    //精度2位,保存到小数点后2位
        public static void main(String[] args) {
            //%[argument_index$][flags][width][.precision]conversion
            //%[argument_index$] : %1$
            //[.precision] : .2f f表示传入的数字是浮点型
            String s = String.format("%1$.2f", 12.12555);
            System.out.println(s);
        }

    位数不够向前补0

        //位数不够向前补0
        public static void main(String[] args) {
            //%[argument_index$][flags][width][.precision]conversion
            //%[argument_index$] : %1$
            //[flags] : 0     不够了补0
            //[width] : 5  字符串长度为5
            //conversion : d 结论被格式化为十进制整数
            String s = String.format("%1$05d", 123);
            System.out.println(s);
        
        }

    位数不够后面补空格,包含中文也可以,如"java格式化"

    //位数不够补空格
        public static void main(String[] args) {
            //%[argument_index$][flags][width][.precision]conversion
            //%[argument_index$] : %1$
            //[flags] : - 在最小宽度内左对齐
            //[width] : 8 8位
            //conversion : s 代表字符串
            String s = String.format("%1$-8s", "abd");
            System.out.println(s);
            //char[] charArray = s.toCharArray();
            //System.out.println("charArray.length = "+ charArray.length);

     

  • 相关阅读:
    控制台内容保存为文件
    SpringBoot
    JAVA基础
    jenkins的.gradle目录结构说明和清理
    macos 签名+公证app生成dmg后,安装使用过程中崩溃
    MacOS命令行打包+签名+公证+生成dmg文件
    jenkins构建调用tar报错:tar: Failed to set default locale
    jenkins构建报错:appdmg: command not found
    jenkins 构建xcode-select -s 切换xcode版本失败 (切换xcode路径无效)
    jenkins 执行shell编译go 代码报错:build cache is required, but could not be located: GOCACHE is not defined and neither $XDG_CACHE_HOME nor $HOME are defined
  • 原文地址:https://www.cnblogs.com/lonely-buffoon/p/5567837.html
Copyright © 2011-2022 走看看