private static double javaNumberFormat(String value, Locale locale) throws ParseException {
ParsePosition parsePosition = new ParsePosition(0);
DecimalFormat numFormat = (java.text.DecimalFormat) NumberFormat.getNumberInstance(locale);
Number number = numFormat.parse(value, parsePosition);
if(number == null || parsePosition.getIndex() != value.length())
throw new ParseException("Invalid input", parsePosition.getIndex());
return number.doubleValue();
}
在pre-android 7中,当你将字符串“123 45”作为“value”传递给上面的方法时,它会抛出parse异常,因为DecimalFormat.parse()为“123 45”返回null . 在Android 7中,该方法返回“12345”,因为DecimalFormat.parse()返回“12345” .
上述行为的原因是因为android团队的以下迁移 . https://developer.android.com/guide/topics/resources/icu4j-framework.html#migration
我正在寻找可以解析给定字符串的DecimalFormat的替代方案,该解决方案还应该考虑区域设置信息 . Double.parseDouble对我有用,但它不考虑区域设置 .