将String转换为Double时无效 [英] Invalid double in converting String to Double

查看:163
本文介绍了将String转换为Double时无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个NumberFormatException:这行代码中的无效双111,000,000:

i get a NumberFormatException : invalid double "111,000,000" in this line of code :

double SalePotential = Double.valueOf(EtPotential.getText().toString());

在开始我使用了数字格式,格式化我的双值分隔号,并将其插入一个EditText,但是当我尝试检索EditText的值时,它会抛出异常:

in the beginning i've used Numberformat , to format my double value for separating number and inserted it to an EditText but when i try to retrieve the value of EditText it throws me the exception :

NumberFormat f = NumberFormat.getInstance();
EtPotential.setText(String.valueOf(f.format(PTData.SalePotential)));

我也尝试过DecimalFormat或Double.parseDouble,没有成功。任何帮助将被欣赏! :

i've also tried DecimalFormat or Double.parseDouble with no Success. any help would be Appreciated! :

DecimalFormat f = new DecimalFormat("###.###", DecimalFormatSymbols.getInstance());
double SalePotential = Double.parseDouble(EtPotential.getText().toString());


推荐答案

在解析

double SalePotential = Double.parseDouble(EtPotential.getText().toString().replace(",", ""));






更新:正确实现


Update : With proper implementation

double salePotential = 0; // Variable name should start with small letter 
try {
    salePotential = Double.parseDouble(EtPotential.getText().toString().replace(",", ""));
} catch (NumberFormatException e) {
    // EditText EtPotential does not contain a valid double
}

快乐编码:)

Happy coding :)

这篇关于将String转换为Double时无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆