public static void main(String s[]) {
getDoubleFromStringWithPercent("12345%");
}
private static Double getDoubleFromStringWithPercent(String string) {
Locale locale = Locale.CANADA;
Double num = null;
try {
Number number = NumberFormat.getPercentInstance(locale).parse(string);
if (number instanceof Long) {
num = ((Long) number).doubleValue();
} else {
num = (Double) number;
}
} catch (ParseException e) {
throw new RuntimeException(e);
}
return num;
}
语言环境需要查找小数分隔符(“.”或“,”);
替代版本:
String string ="2165%";
double d = Double.parseDouble(string.substring(0, string.length()-1));
if (d != 0) {
d = d / 100;
}