Android 获取双变量值时发生致命异常
FATAL EXCEPTION in Android When getting Double Varible Values
当用户在应用程序中输入折扣率时,我使用一个按钮来计算折扣。当我得到低于 1000 的总价值时,它工作正常并提供折扣。但如果总值大于 1000,它会给出致命异常并使应用程序崩溃。你能帮我解决这个问题吗?
谢谢
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Double discountRate = Double.parseDouble(input_rate.getText().toString());
Double discount = 0.0;
if(discountRate.equals("")){
Toast.makeText(CreateInvoiceActivity.this, "Discount Rate is Empty", Toast.LENGTH_SHORT).show();
}
else {
Double totalValue = Double.parseDouble(invoiceTotal.getText().toString());
try {
discount = (totalValue * (discountRate / 100));
Toast.makeText(CreateInvoiceActivity.this, "Discount is" + discount, Toast.LENGTH_SHORT).show();
}catch (NumberFormatException ignore){}
}
}
});
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.antlergroup.aits.ierunt.ai, PID: 26795
java.lang.NumberFormatException: For input string: "1,008.00"
at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1306)
at java.lang.Double.parseDouble(Double.java:547)
at com.antlergroup.aits.ierunt.ai.view.activity.CreateInvoiceActivity.onClick(CreateInvoiceActivity.java:291)
at android.view.View.performClick(View.java:5647)
at android.view.View$PerformClick.run(View.java:22479)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6623)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
I/Process: Sending signal. PID: 26795 SIG: 9
您应该删除千位分隔符(逗号)。
Double totalValue = Double.parseDouble(invoiceTotal.getText().toString().replace(",", ""));
您还应该:
- 首选java.text.NumberFormat解析数字
- 使用正确的语言环境
- 验证输入
当用户在应用程序中输入折扣率时,我使用一个按钮来计算折扣。当我得到低于 1000 的总价值时,它工作正常并提供折扣。但如果总值大于 1000,它会给出致命异常并使应用程序崩溃。你能帮我解决这个问题吗? 谢谢
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Double discountRate = Double.parseDouble(input_rate.getText().toString());
Double discount = 0.0;
if(discountRate.equals("")){
Toast.makeText(CreateInvoiceActivity.this, "Discount Rate is Empty", Toast.LENGTH_SHORT).show();
}
else {
Double totalValue = Double.parseDouble(invoiceTotal.getText().toString());
try {
discount = (totalValue * (discountRate / 100));
Toast.makeText(CreateInvoiceActivity.this, "Discount is" + discount, Toast.LENGTH_SHORT).show();
}catch (NumberFormatException ignore){}
}
}
});
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.antlergroup.aits.ierunt.ai, PID: 26795
java.lang.NumberFormatException: For input string: "1,008.00"
at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1306)
at java.lang.Double.parseDouble(Double.java:547)
at com.antlergroup.aits.ierunt.ai.view.activity.CreateInvoiceActivity.onClick(CreateInvoiceActivity.java:291)
at android.view.View.performClick(View.java:5647)
at android.view.View$PerformClick.run(View.java:22479)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6623)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
I/Process: Sending signal. PID: 26795 SIG: 9
您应该删除千位分隔符(逗号)。
Double totalValue = Double.parseDouble(invoiceTotal.getText().toString().replace(",", ""));
您还应该:
- 首选java.text.NumberFormat解析数字
- 使用正确的语言环境
- 验证输入