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

  • 相关阅读:
    栈和队列的概念
    01-开始使用django(全、简)
    临时记录01
    centos删除乱码名称的文件
    《计算机网络》谢希仁(第7版) 第一章
    git提交到远程虚拟机
    安全篇:弱密码python检测工具
    正向代理、Nginx(反向代理、负载均衡、静态资源服务器)
    列表去重、去除满足一定条件的元素
    editplus的常用快捷键
  • 原文地址:https://www.cnblogs.com/shihaiming/p/5979740.html
Copyright © 2011-2022 走看看