zoukankan      html  css  js  c++  java
  • JAVA中的Formatter

    package com.st.day20150525;
    
    import java.util.Formatter;
    
    public class StringTest02 {
        /**
         *    %[argument_index$][flags][width][.precision]conversion
         *    具体的参数解释,可以查看对应的API
         */
        private static double total = 0 ;
        private static Formatter formatter = new Formatter(System.out); 
        public static void printTitle(){
            formatter.format("%-15s %-5s %-10s
    ", "Item","Qty","Price");
            formatter.format("%-15s %-5s %-10s
    ", "-----","---","----");
        }
        
        public static void print(String name,int qty,double price){
            formatter.format("%-15s %-5d %-10.2f
    ", name,qty,price);
            total += price ;
        }
        
        public static void printTotal(){
            formatter.format("%-15s %-5s %-10.2f
    ", "Tex","",total*0.6);
            formatter.format("%-15s %-5s %-10s
    ", "","","-----");
            formatter.format("%-15s %-5s %-10.2f
    ", "Total","",total*1.6);
        }
        public static void main(String[] args) {
            printTitle();
            print("apple", 3, 2.5);
            print("orange", 1, 1.2);
            print("banana", 10, 0.5);
            printTotal();
            
            //任何类型都可以b(布尔类型)进行转换,只有为null的时候返回false
            formatter.format("%b
    ", 0);
            formatter.format("%b
    ", "");
            formatter.format("%b
    ", null);
            // 如果只是简单的字符串格式化,可以使用String.format 
            // 其内部实现new Formmatter对象
            System.out.println(String.format("%-15s %-5s %-10s", "我可以","做","一样的事情"));
            
        }
    }
  • 相关阅读:
    qcow2文件压缩
    raw格式镜像文件压缩并转换为qcow2格式
    centos7 install virt-sysprep
    镜像简介
    QEMU 使用的镜像文件:qcow2 与 raw
    ubuntu14.04中国源
    less css下载及编绎工具
    分布式计算中WebService的替代方案: RPC (XML-RPC | JSON-RPC)
    Asp.net WebServer
    C#取调用堆栈StackTrace
  • 原文地址:https://www.cnblogs.com/xiaotao726/p/4628781.html
Copyright © 2011-2022 走看看