DecimalFormat的用法介绍

import java.text.*;
import java.util.*;java

public class DecimalFormatSample {
 public static void main(String args[]) {
  DecimalFormat myformat1 = new DecimalFormat("###,###.0000");//使用系统默认的格式
  System.out.println(myformat1.format(111111123456.12));app

  Locale.setDefault(Locale.US);
  DecimalFormat myformat2 = new DecimalFormat("###,###.0000");//使用美国的格式
  System.out.println(myformat2.format(111111123456.12));orm

  //----------------------------also use applypattern------------------------------//对象

  DecimalFormat myformat3 = new DecimalFormat();
  myformat3.applyPattern("##,###.000");
  System.out.println(myformat3.format(11112345.12345));
//-----------------控制指数输出-------------------------------------------------//ci

     DecimalFormat myformat4 = new DecimalFormat();
  myformat4.applyPattern("0.000E0000");
  System.out.println(myformat4.format(10000));
  System.out.println(myformat4.format(12345678.345));
//------------------百分数的输出-------------------------------------------//
/*     DecimalFormat是NumberFormat的一个子类,其实例被指定为特定的地区。所以,你能够使用NumberFormat.getInstance 指定一个地区,
而后将结构强制转换为一个DecimalFormat对象。文档中提到这个技术能够在大多状况下适用,可是你须要用try/catch 块包围强制转换以防转
换不能正常工做 (大概在很是不明显得状况下使用一个奇异的地区)。    */
       DecimalFormat myformat5 = null;
  try{
      myformat5 = (DecimalFormat)NumberFormat.getPercentInstance();
  }catch(ClassCastException e)
  {
   System.err.println(e); 
  }
  myformat5.applyPattern("00.0000%");
  System.out.println(myformat5.format(0.34567));
  System.out.println(myformat5.format(1.34567));文档

      
 
 }get

/*---------------------------------运行结果-------------------------------------------//
 F:\2004-04-12>java DecimalFormatSample
111,111,123,456.1200
111,111,123,456.1200
11,112,345.123
1.000E0004
1.235E0007
34.5670%
134.5670%io

*/ast

}form

相关文章
相关标签/搜索