【Java常用類庫】_NumberFormat

來源:互聯網
上載者:User

【Java常用類庫】_NumberFormat

import java.text.* ;public class NumberFormatDemo01{    public static void main(String args[]){        NumberFormat nf = null ;        // 聲明一個NumberFormat對象        nf = NumberFormat.getInstance() ;    // 得到預設的數字格式化顯示        System.out.println("格式化之後的數字:" + nf.format(10000000)) ;        System.out.println("格式化之後的數字:" + nf.format(1000.345)) ;    }};

輸出:

格式化之後的數字:10,000,000
格式化之後的數字:1,000.345

執行個體化模板:

import java.text.* ;class FormatDemo{    public void format1(String pattern,double value){    // 此方法專門用於完成數位格式化顯示        DecimalFormat df = null ;            // 聲明一個DecimalFormat類的對象        df = new DecimalFormat(pattern) ;    // 執行個體化對象,傳入模板        String str = df.format(value) ;        // 格式化數字        System.out.println("使用" + pattern            + "格式化數字" + value + ":" + str) ;    }};public class NumberFormatDemo02{    public static void main(String args[]){        FormatDemo demo = new FormatDemo() ;    // 格式化對象的類        demo.format1("###,###.###",111222.34567) ;        demo.format1("000,000.000",11222.34567) ;        demo.format1("###,###.###¥",111222.34567) ;        demo.format1("000,000.000¥",11222.34567) ;        demo.format1("##.###%",0.345678) ;        demo.format1("00.###%",0.0345678) ;        demo.format1("###.###\u2030",0.345678) ;    }};

輸出:

使用###,###.###格式化數字111222.34567:111,222.346
使用000,000.000格式化數字11222.34567:011,222.346
使用###,###.###¥格式化數字111222.34567:111,222.346¥
使用000,000.000¥格式化數字11222.34567:011,222.346¥
使用##.###%格式化數字0.345678:34.568%
使用00.###%格式化數字0.0345678:03.457%
使用###.###‰格式化數字0.345678:345.678‰

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.