This question already has answers here:
Best way to parseDouble with comma as decimal separator?
(9个答案)
2个月前关闭。
我在某些设备(主要是API29)中遇到此异常。因此,请为“切换”按钮分配双舍入值。当用户单击它们时,它将把该值添加到当前总计中,然后将该总计转换为String以便在TextView中显示。
我尝试添加异常,但是它无法显示分配给切换按钮的双倍价格。
另外,我在这里查看了其他一些线程,这些线程对我们没有帮助。
代码如下;有什么指导吗?
首先用replace将逗号转换为点:
(9个答案)
2个月前关闭。
我在某些设备(主要是API29)中遇到此异常。因此,请为“切换”按钮分配双舍入值。当用户单击它们时,它将把该值添加到当前总计中,然后将该总计转换为String以便在TextView中显示。
我尝试添加异常,但是它无法显示分配给切换按钮的双倍价格。
另外,我在这里查看了其他一些线程,这些线程对我们没有帮助。
代码如下;有什么指导吗?
private void setListeners() {
//boolean chemoIsChecked = false;
chemoBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked) {
try{
Double chemoPrice = Double.valueOf(chemoButtonPrice);
d = chemoPrice + d;
String totalPrice = String.valueOf(String.format(Locale.GERMAN, "%.2f", d));
premiumTitle.setText("Premie per maand incl. BTW: € " + totalPrice);
}catch (NumberFormatException e){
}
} else {
try{
}catch (NumberFormatException e){
Double chemoPrice = Double.valueOf(chemoButtonPrice);
d = d - chemoPrice;
String totalPrice = String.valueOf(String.format(Locale.GERMAN,"%.2f", d));
premiumTitle.setText("Premie per maand incl. BTW: € " + totalPrice);
}
}
}
});
cremBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked) {
Double cremPrice = Double.valueOf(cremationButtonPrice);
d = cremPrice + d;
String totalPrice = String.valueOf(String.format(Locale.GERMAN,"%.2f", d));
premiumTitle.setText("Premie per maand incl. BTW: € " + totalPrice);
} else {
Double cremPrice = Double.valueOf(cremationButtonPrice);
d = d - cremPrice;
String totalPrice = String.valueOf(String.format(Locale.GERMAN,"%.2f", d));
premiumTitle.setText("Premie per maand incl. BTW: € " + totalPrice);
}
}
});
travenBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked) {
Double travelPrice = Double.valueOf(travelButtonPrice);
d = travelPrice + d;
String totalPrice = String.valueOf(String.format(Locale.GERMAN,"%.2f", d));
premiumTitle.setText("Premie per maand incl. BTW: € " + totalPrice);
} else {
Double travelPrice = Double.valueOf(travelButtonPrice);
d = d - travelPrice;
String totalPrice = String.valueOf(String.format(Locale.GERMAN,"%.2f", d));
premiumTitle.setText("Premie per maand incl. BTW: € " + totalPrice);
}
}
});
btnContinue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
createAdditionalPackages();
}
Fatal Exception: java.lang.NumberFormatException: For input string: "3,78"
at sun.misc.FloatingDecimal.readJavaFormatString + 2043(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble + 110(FloatingDecimal.java:110)
at java.lang.Double.parseDouble + 538(Double.java:538)
at java.lang.Double.valueOf + 502(Double.java:502)
at nl.risk.www.smart2coverapp.activity.PetplanAdditionalPlansActivity$2.onCheckedChanged + 165(PetplanAdditionalPlansActivity.java:165)
at android.widget.CompoundButton.setChecked + 182(CompoundButton.java:182)
at android.widget.ToggleButton.setChecked + 71(ToggleButton.java:71)
at android.widget.CompoundButton.toggle + 136(CompoundButton.java:136)
at android.widget.CompoundButton.performClick + 141(CompoundButton.java:141)
at android.view.View.performClickInternal + 7312(View.java:7312)
at android.view.View.access$3200 + 846(View.java:846)
at android.view.View$PerformClick.run + 27794(View.java:27794)
at android.os.Handler.handleCallback + 873(Handler.java:873)
at android.os.Handler.dispatchMessage + 99(Handler.java:99)
at android.os.Looper.loop + 214(Looper.java:214)
at android.app.ActivityThread.main + 7099(ActivityThread.java:7099)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 494(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main + 965(ZygoteInit.java:965)
最佳答案
而不是做:
Double travelPrice = Double.valueOf(travelButtonPrice);
首先用replace将逗号转换为点:
Double travelPrice = Double.valueOf(travelButtonPrice.replace(",", "."));