Android中DecimalFormat的用法
2010-02-20 18:02
369 查看
import java.text.*;
import java.util.*;
public class DecimalFormatSample {
public static void main(String args[]) {
DecimalFormat myformat1 = new DecimalFormat("###,###.0000");//使用系统默认的格式
System.out.println(myformat1.format(111111123456.12));
Locale.setDefault(Locale.US);
DecimalFormat myformat2 = new DecimalFormat("###,###.0000");//使用美国的格式
System.out.println(myformat2.format(111111123456.12));
//----------------------------also use applypattern------------------------------//
DecimalFormat myformat3 = new DecimalFormat();
myformat3.applyPattern("##,###.000");
System.out.println(myformat3.format(11112345.12345));
//-----------------控制指数输出-------------------------------------------------//
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));
}
/*---------------------------------运行结果-------------------------------------------//
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%
*/
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zgqtxwd/archive/2008/04/25/2326123.aspx
import java.util.*;
public class DecimalFormatSample {
public static void main(String args[]) {
DecimalFormat myformat1 = new DecimalFormat("###,###.0000");//使用系统默认的格式
System.out.println(myformat1.format(111111123456.12));
Locale.setDefault(Locale.US);
DecimalFormat myformat2 = new DecimalFormat("###,###.0000");//使用美国的格式
System.out.println(myformat2.format(111111123456.12));
//----------------------------also use applypattern------------------------------//
DecimalFormat myformat3 = new DecimalFormat();
myformat3.applyPattern("##,###.000");
System.out.println(myformat3.format(11112345.12345));
//-----------------控制指数输出-------------------------------------------------//
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));
}
/*---------------------------------运行结果-------------------------------------------//
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%
*/
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zgqtxwd/archive/2008/04/25/2326123.aspx
相关文章推荐
- android DecimalFormat的用法介绍
- Android中DecimalFormat的用法
- Android Service用法详解
- android:selector的用法
- android中shape,selector,layer-list用法总结
- 【Android】BroadcastReceiver简介和用法实例,两种注册方式 (二)
- ANDROID application详解与用法
- Android之Handler用法总结
- Android之Adapter用法总结
- android 异步任务的用法详解
- Android之Adapter用法总结
- Android 中的DisplayMetrics的用法
- [Android 插件化(二)] DroidPlugin 用法
- java decimalFormat 的用法
- Android开发实践:实战演练隐式Intent的用法
- android ActionBar用法详解
- Android中使用File的时候File.separator的用法
- Android-NDK开发之基础--Android.mk的用法和基础(转CSDN)
- Android TabHost用法详解
- Android学习记录(7)—Intent中显示意图和隐式意图的用法