时在Java中,我试图让DecimalFormat的执行对指数的标志符号产生误差。当它是积极的,我需要一个加号出现。从我读过的这段话看来,这似乎毫无意义,但对我自己来说总是会引发一个错误。我很欣赏可能有其他方法来实现我的目标,但我想了解为什么在这种特定方法中发生错误。的Java DecimalFormat的执行指数符号
Double result = 123.456;
String sresult;
//This works
NumberFormat formatter = new DecimalFormat("0.00000E00");
sresult = formatter.format(result);
System.out.println(sresult); //1.23456E02
//This doesn't work
formatter = new DecimalFormat("0.00000E+00"); //Want to enforce the sign to appear
sresult = formatter.format(result);
System.out.println(sresult); //Expected 1.23456E+02 but error occurs
它抛出了错误:
Exception in thread "main" java.lang.IllegalArgumentException: Malformed exponential pattern "0.00000E+00" at java.text.DecimalFormat.applyPattern(Unknown Source) at java.text.DecimalFormat.(Unknown Source) at deccheck.main(deccheck.java:13)
我明白任何见解。 谢谢, 马克
重新格式化的代码;如果不正确请回复。 – trashgod 2010-08-06 14:49:43