将字母数字字符串转换为双精度数字

问题描述 投票:0回答:2

我正在尝试将字母数字字符串转换为double,并想知道是否有任何简单的方法,例如atoi可用于java中的double。>

样本字符串:"16.78%" => 16.78

我尝试过Double.parseDouble(),但收到错误java.lang.NumberFormatException: Invalid double: "16.78%"

我正在尝试将字母数字字符串转换为double,并想知道是否有任何简单的方法(如atoi)可用于java示例字符串中的double:“ 16.78%” => 16.78我尝试了Double ....

java string string-conversion
2个回答
1
投票
问题是您的字符串中仍然还有百分号。 Double.parseDouble不允许字符串中的任何字符(数字除外)。

0
投票
try { final Number number = new DecimalFormat().parse(alphanumericString); if (number != null) { result = number.floatValue(); } else { throw new ParseException("Parser returned null", 0); } } catch (final ParseException parseException) { DLog.warnf( "Encountered ParseException with converting %s to double. Exception message: %s", alphanumericString, parseException.getMessage()); }
© www.soinside.com 2019 - 2024. All rights reserved.