【发布时间】:2019-02-12 20:46:43
【问题描述】:
我的字符串是“1234567”,我想得到两个像这样的大十进制对象 12345,67 和 12.345,67 但我做不到。代码如下:
DecimalFormat decimalFormat = new DecimalFormat("#,##0.###");
decimalFormat.setParseBigDecimal(true);
String n = decimalFormat.format(1234567);
BigDecimal bigDecimal = (BigDecimal)decimalFormat.parse(n);
DecimalFormat decimalFormat1 = new DecimalFormat("#,0#");
decimalFormat1.setParseBigDecimal(true);
String n1 = decimalFormat.format(1234567);
BigDecimal bigDecimal1 = (BigDecimal)decimalFormat.parse(n1);
System.out.println(n);
System.out.println(bigDecimal);
System.out.println(n1);
System.out.println(bigDecimal1);
输出:
1.234.567,00
1234567.00
1.234.567,00
1234567.00
预期输出:
12.345,67
12.345,67
12345,67
12345,67
感谢您的帮助
【问题讨论】:
-
看起来您的输出和代码不匹配。确保您上传了最新的代码/结果。当我运行你的代码时,我在 "new DecimalFormat("#,0#")" 处得到格式错误的模式 "#,0#"
-
该代码根本不为我运行,它在运行时抛出此异常:
java.lang.IllegalArgumentException: Malformed pattern "#,0#"
标签: java number-formatting bigdecimal