首页 > 解决方案 > Convert CFA to EURO

问题描述

I have a little problem of conversion in an app i am making.

I am getting the decimal value from an edittext and then i convert the value to another currency like you can see in the method.

Actually, when the edittext does not contain a "," it work pretty good.

For example, 258782 from the edittext i can get the conversion but when i add a "," for example 258782,52 it does not work and display nothing.

An idea?

 public void convertCFAtoEURO(){
    double cfa = 0;
    double euro = 0;
    String eur = "";
    String str = "";
    if(editTextCFA.length() > 0) {
        imvd.setEnabled(true);
        try
        {
            String textCFA = editTextCFA.getText().toString();
            String changed = textCFA.replace(",",".");
            cfa = Double.valueOf(changed);
            euro = cfa / (double) 654.67;
            eur = String.valueOf(euro);
            editTextEURO.setText(bigDecimalData(eur));

        } catch (Exception e1) {
            // this means it is not double
            e1.printStackTrace();
        }

    }

}

标签: javaandroid

解决方案


这是 bigDecimal 方法。就是每三位数分开数位,限制小数点后两位

public static String bigDecimalData(String data) {
    if (!TextUtils.isEmpty(data)) {
        BigDecimal bd = new BigDecimal(Double.parseDouble(data));
        DecimalFormat df = new DecimalFormat(",###,###.00");
        return df.format(bd);
    }
    return "";
}

推荐阅读