某些设备无法解析ParseDouble

时间:2014-05-20 05:06:02

标签: java android

我正在使用NumberTextWatcher进行EditText的实时编辑,这对我来说很好, 但有些用户报告说他们遇到了NumberException问题,我认为这是因为EditText中的数字在123,456中发生了变化而无法解析为双倍,所以我做了一些更改并使用了Replace("," , "")代码也不适合一些人! 我应该怎么做才能让这个代码适用于我的所有用户? 谢谢,抱歉我的英语不好:D

NumberTextWatcher

public NumberTextWatcher(EditText et)
{
    df = new DecimalFormat("#,###");
    df.setDecimalSeparatorAlwaysShown(true);
    dfnd = new DecimalFormat("#,###");
    this.et = et;
    hasFractionalPart = false;
}

@SuppressWarnings("unused")
private static final String TAG = "NumberTextWatcher";

@Override
public void afterTextChanged(Editable s)
{
    et.removeTextChangedListener(this);

    try {
        int inilen, endlen;
        inilen = et.getText().length();

        String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "");
        Number n = df.parse(v);
        int cp = et.getSelectionStart();
        if (hasFractionalPart) {
            et.setText(df.format(n));
        } else {
            et.setText(dfnd.format(n));
        }
        endlen = et.getText().length();
        int sel = (cp + (endlen - inilen));
        if (sel > 0 && sel <= et.getText().length()) {
            et.setSelection(sel);
        } else {
            // place cursor at the end?
            et.setSelection(et.getText().length() - 1);
        }
    } catch (NumberFormatException nfe) {
        // do nothing?
    } catch (ParseException e) {
        // do nothing?
    }

    et.addTextChangedListener(this);
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
    if (s.toString().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator())))
    {
        hasFractionalPart = true;
    } else {
        hasFractionalPart = false;
    }
}

这是 MainActivity

final EditText amount = (EditText) findViewById(R.id.editText1);
final EditText duration = (EditText) findViewById(R.id.editText2);
final EditText interest = (EditText) findViewById(R.id.editText3);
amount.addTextChangedListener(new NumberTextWatcher(amount));
duration.addTextChangedListener(new NumberTextWatcher(duration));
interest.addTextChangedListener(new NumberTextWatcher(interest));
String amount1 = amount.getText().toString().replaceAll(",", "");
String duration1 = duration.getText().toString().replaceAll(",", "");
String interest1 = interest.getText().toString().replaceAll(",", "");
try{
double i = Double.parseDouble(amount1);
double j = Double.parseDouble(duration1);
double z = Double.parseDouble(interest1);
}
catch(NumberFormatException e){

 }

2 个答案:

答案 0 :(得分:1)

程序以与语言环境无关的方式存储和操作数字。在显示或打印数字之前,程序必须将其转换为区域设置敏感格式的字符串。例如,在法国,数字123456.78的格式应为123 456,78,在德国则应为123.456,78。因此,您无法取代&#34;,&#34;和&#34;。&#34;。

以下代码是如何使用美国用户的当前货币从double和back转换的示例。

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Currency;
import java.util.Locale;

public class Test
{
    public static void main(String[] args)
    {
        Locale currentLocale = Locale.getDefault();
        Currency currentCurrency = Currency.getInstance(currentLocale);
        Double currencyAmount = new Double(9876543.21);
        NumberFormat currencyFormatter = 
                NumberFormat.getCurrencyInstance(currentLocale);
        System.out.println(NumberFormat.getNumberInstance(currentLocale));
        System.out.println(
                           currentLocale.getDisplayName() + ", " +
                           currentCurrency.getDisplayName() + ": " +
                           currencyFormatter.format(currencyAmount));

        try
        {
            System.out.println(currencyFormatter.parseObject("$9,876,543.21"));
        }
        catch (ParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

你还能做什么:
*确保用户输入格式正确的数字。例如,除了数字之外,你不应该看到任何其他字符。 + - $等。
*如果您知道用户将始终输入两位小数,例如,10美元将表示为10.00(或10,00),那么您可以安全地删除所有&#34;,&#34;和&#34;。&#34;并通过除以100得到原始数字,但记住也要删除空格(记得法国:))

另外,请查看文档:{​​{3}}

答案 1 :(得分:0)

问题可能是其他用户可能正在输入格式错误的数字。所以你只是用白色空格替换逗号。也许你应该替换所有非数字字符?

amount1 = amount.getText().toString().replaceAll("[^\\d]", "");

或者如果您希望它们留下小数点,请使用以下内容:

amount1 = amount.getText().toString().replaceAll("[^\\d.]", "");