欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

【Java常用类库】_NumberFormat,java_numberformat

来源: javaer 分享于  点击 41199 次 点评:249

【Java常用类库】_NumberFormat,java_numberformat


【Java常用类库】_NumberFormat

分类: Java
【Java常用类库】_NumberFormat

[java] view plaincopyprint?
  1. import java.text.* ;  
  2. public class NumberFormatDemo01{  
  3.     public static void main(String args[]){  
  4.         NumberFormat nf = null ;        // 声明一个NumberFormat对象  
  5.         nf = NumberFormat.getInstance() ;    // 得到默认的数字格式化显示  
  6.         System.out.println("格式化之后的数字:" + nf.format(10000000)) ;  
  7.         System.out.println("格式化之后的数字:" + nf.format(1000.345)) ;  
  8.     }  
  9. };  


输出:

格式化之后的数字:10,000,000
格式化之后的数字:1,000.345



实例化模板:



[java] view plaincopyprint?
  1. import java.text.* ;  
  2. class FormatDemo{  
  3.     public void format1(String pattern,double value){    // 此方法专门用于完成数字的格式化显示  
  4.         DecimalFormat df = null ;            // 声明一个DecimalFormat类的对象  
  5.         df = new DecimalFormat(pattern) ;    // 实例化对象,传入模板  
  6.         String str = df.format(value) ;        // 格式化数字  
  7.         System.out.println("使用" + pattern  
  8.             + "格式化数字" + value + ":" + str) ;  
  9.     }  
  10. };  
  11. public class NumberFormatDemo02{  
  12.     public static void main(String args[]){  
  13.         FormatDemo demo = new FormatDemo() ;    // 格式化对象的类  
  14.         demo.format1("###,###.###",111222.34567) ;  
  15.         demo.format1("000,000.000",11222.34567) ;  
  16.         demo.format1("###,###.###¥",111222.34567) ;  
  17.         demo.format1("000,000.000¥",11222.34567) ;  
  18.         demo.format1("##.###%",0.345678) ;  
  19.         demo.format1("00.###%",0.0345678) ;  
  20.         demo.format1("###.###\u2030",0.345678) ;  
  21.     }  
  22. };  

输出:

使用###,###.###格式化数字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‰

相关文章

    暂无相关文章
相关栏目:

用户点评