zoukankan      html  css  js  c++  java
  • String.format格式化

    package hb.strformat;
    
    public class IntFormat {
    
        public static void main(String[] args) {
            
            int d = Integer.parseInt("99099");
            System.out.println(d);
            //格式化为整形型字符串
            System.out.println(String.format("%d",d));
            //整数长度为7,如果不到7位就用0填充
            System.out.println(String.format("%07d",9909));
            System.out.println(String.format("%07d",99099999));
            //长度不满7就用空格填充
            System.out.println(String.format("% 7d",9909));
            //使用,对数字分组
            System.out.println(String.format("%,d",9909999));
            //显示正负数
            System.out.println(String.format("%+d",d));
            System.out.println(String.format("%+d",-345));
        }
    
    }
    package hb.strformat;
    
    public class FloatFormat {
    
        public static void main(String[] args) {
            
            double d = Double.parseDouble("9909999999.9999");
            System.out.println(d);
            //格式化为浮点型字符串
            System.out.println(String.format("%f",d));
            //整数部分全部显示,小数部分后面保留5位小数
            System.out.println(String.format("%.5f",d));
            //使用,对数字分组
            System.out.println(String.format("%,f",d));
            //显示正负数
            System.out.println(String.format("%+f",d));
            System.out.println(String.format("%+f",-345.98));
            //算小数点后面的位数一起时15
            System.out.println(String.format("%015f",345.98));
            //小数点后面保留三位小数
            System.out.println(String.format("%015.3f",345.98));
        }
    
    }
    package hb.strformat;
    
    import java.util.Date;
    
    public class DateFormat {
    
        //%tx使用来专门格式化日期和时间的
        public static void main(String[] args) {
            Date now = new Date();
            System.out.println("全部日期和时间信息:"+String.format("%tc", now));
            System.out.println("年-月-日格式:"+String.format("%tF", now));
            System.out.println("月/日/年格式:"+String.format("%tD", now));
            System.out.println("HH:MM :"+String.format("%tR", now));
            System.out.println("HH:MM:SS PM格式(12时制):"+String.format("%tr", now));
            System.out.println("HH:MM:SS格式(24时制):"+String.format("%tT", now));
        }
    
    }

    原文:http://hbiao68.iteye.com/blog/1769053

  • 相关阅读:
    Shell编程进阶 1.2 shell结构及执行
    LNMP 1.6 常见的502问题解决
    关于贴图看不到。显示是白色或者其他。
    windows 任务栏图标宽度固定
    Install Oracle Java JDK/JRE 7u55 on Fedora 20/19, CentOS/RHEL 6.5/5.10
    盘点天龙历史:七年以来所有资料片
    linux shell 逻辑运算符、逻辑表达式详解
    vim 把满足条件的数字进行加上一些数字
    win7 一些快捷系统工具命令
    Linux下用C读取配置文件。类似ini这样。
  • 原文地址:https://www.cnblogs.com/shihaiming/p/5979740.html
Copyright © 2011-2022 走看看