java - 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();
}
}
}
解决方案
这是 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 "";
}
推荐阅读
- laravel - 如何将查询生成器转换为 Elequent 查询?
- javascript - ExtJS 类命名约定
- java - 带有模拟测试的 Dgs 框架?
- reactjs - 带有图像的 LandingPage 屏幕无法在我的 Android 手机上加载
- go - Golang:覆盖切片的初始元素
- machine-learning - predict_proba 方法如何适用于 sklearn 中 DecisionTreeClassifier 中的输入样本 X?
- makefile - Gnu Make:调用并行make时,如果在构建过程中提供了先决条件,make会尝试重新制作这些吗?
- angular - 从谷歌驱动器而不是本地机器上传文件
- android - Appium Native View,如何点击按钮返回上一屏
- .htaccess - 配置正确的 htaccess 规则