gle*_*ien 0 java number-formatting
我正在努力在 Java 中格式化数字。
我的输入格式如下(我无法更改):2.000,15 ,输出应如下所示:2000.15
如果逗号后只有 0,则输出应如下所示:2000
输入以字符串形式给出。
我已经尝试使用 aDecimalFormat但这只会导致IllegalArgumentException
DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance(Locale.ENGLISH);
decimalFormat.setMinimumFractionDigits(0);
decimalFormat.setMaximumFractionDigits(2);
decimalFormat.setGroupingUsed(false);
return decimalFormat.format(Double.parseDouble(characteristicValue));
Run Code Online (Sandbox Code Playgroud)
试试这个:
NumberFormat fmt = NumberFormat.getInstance(Locale.GERMAN);
try {
BigDecimal bd = new BigDecimal(fmt.parse("2.000,15").toString());
System.out.println(bd.toString()); // output: 2000.15
} catch (ParseException e) {
//ex handling
}
Run Code Online (Sandbox Code Playgroud)