怎样使用java.text.DecimalFormat?
将数字进行格式化,比如取2位小数,这是最常见的。Java 提供 DecimalFormat类,帮你用最快的速度将数字格式化需要的样子。下面是一个例子:
[java] view plaincopyprint?
importjava.text.DecimalFormat;
publicclassTestNumberFormat{
publicstaticvoidmain(String[]args){
doublepi=3.1415927;//圆周率
//取一位整数
System.out.println(newDecimalFormat("0").format(pi));//3
//取一位整数和两位小数
System.out.println(newDecimalFormat("0.00").format(pi));//3.14
//取两位整数和三位小数,整数不足部分以0填补。
System.out.println(new DecimalFormat("00.000").format(pi));// 03.142
//取所有整数部分
System.out.println(newDecimalFormat("#").format(pi));//3
//以百分比方式计数,并取两位小数
System.out.println(new DecimalFormat("#.##%").format(pi));//314.16%
longc=299792458;//光速
//显示为科学计数法,并取五位小数
System.out.println(newDecimalFormat("#.#####E0").format(c));//2.99792E8
//显示为两位整数的科学计数法,并取四位小数
System.out.println(newDecimalFormat("00.####E0").format(c));//29.9792E7
//每三位以逗号进行分隔。
System.out.println(newDecimalFormat(",###").format(c));//299,792,458
//将格式嵌入文本
System.out.println(newDecimalFormat("光速大小为每秒,###米。").format(c));
DecimalFormat 类主要靠 # 和 0 两种占位符号来指定数字长度。0 表示如果位数不足则以 0 填充,# 表示只要有可能就把数字拉上这个位置。上面的例子包含了差不多所有的基本用法.decimalformat 是 numberformat 的一个具体子类,用于格式化十进制数字。该类设计有各种功能,使其能够解析和格式化任意语言环境中的数,包括对西方语言、阿拉伯语和印度语数字的支持。它还支持不同类型的数,包括整数 (123)、定点数 (123.4)、科学记数法表示的数 (1.23e4)、百分数 (12%) 和金额 ($123)。所有这些内容都可以本地化。
要获取具体语言环境的 numberformat(包括默认语言环境),可调用 numberformat 的某个工厂方法,如 getinstance()。通常不直接调用 decimalformat 的构造方法,因为 numberformat 的工厂方法可能返回不同于 decimalformat 的子类。
// print out a number using the localized number, integer, currency,
// and percent format for each locale
locale[] locales = numberformat.getavailablelocales();
double mynumber = -1234.56;
numberformat form;
for (int j=0; j<4; ++j) {
system.out.println("format");
for (int i = 0; i < locales.length; ++i) {
if (locales[i].getcountry().length() == 0) {
continue; // skip language-only locales
}
system.out.print(locales[i].getdisplayname());
switch (j) {
case 0:
form = numberformat.getinstance(locales[i]); break;
case 1:
form = numberformat.getintegerinstance(locales[i]); break;
case 2:
form = numberformat.getcurrencyinstance(locales[i]); break;
default:
form = numberformat.getpercentinstance(locales[i]); break;
}
if (form instanceof decimalformat) {
system.out.print(": " + ((decimalformat) form).topattern());
}
system.out.print(" -> " + form.format(mynumber));
try {
system.out.println(" -> " + form.parse(form.format(mynumber)));
} catch (parseexception e) {}
}
DecimalFormat x =new DecimalFormat("0.00") 的用法
public static void main(String[] args){
String windows7 = "Enter the principal, rate, and period:";
// String input = JOptionPane.showInputDialog(windows7);
System.out.println(windows7);
Scanner sc = new Scanner(System.in);
double principal = sc.nextDouble();
double rate = sc.nextDouble();
double period = sc.nextDouble();
//compute result
double result = principal * Math.pow( 1+rate, period );
DecimalFormat x =new DecimalFormat("0.00") ;
// display result
String s ="balance after 20 years" + " = "+ x.format(result);
JOptionPane.showMessageDialog(null, s);
}虽然我很聪明,但这么说真的难到我了