public class Test {
public static void main(String[] args) {
final String test1 = new String("01,");
final String test2 = new String("01,0");
final String test3 = new String("1,00");
String pattern = "##,##";
DecimalFormat formatter;
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setGroupingSeparator(',');
formatter = new DecimalFormat(pattern, dfs);
String result1 = formatter.format(test1);
String result2 = formatter.format(test2);
String result3 = formatter.format(test3);
System.out.println("Result 1 == " + result1);
System.out.println("Result 2 == " + result2);
System.out.println("Result 3 == " + result3);
}
}
我正在尝试格式化字符串。我添加了用于格式化的代码。我得到了例外。
对于所有这些,我希望结果为 01,00。
例外 -
Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Number
at java.text.DecimalFormat.format(DecimalFormat.java:487)
at java.text.Format.format(Format.java:140)
at com.test.Test.main(Test.java:21)
如果有人有任何想法,请指导我。