每当有非零余数时,我的Bigdecimal分区会导致app崩溃?

时间:2014-03-09 16:58:41

标签: java android bigdecimal

我有这个代码来执行

private String evaluateResult(boolean requestedByUser) {
    if ((!requestedByUser && mOperationStack.size() != 4)
            || (requestedByUser && mOperationStack.size() != 3))
        return null;

    String left = mOperationStack.get(0);
    String operator = mOperationStack.get(1);
    String right = mOperationStack.get(2);
    String tmp = null;
    if (!requestedByUser)
        tmp = mOperationStack.get(3);

    BigDecimal leftVal = new BigDecimal(left);
    BigDecimal rightVal = new BigDecimal(right);
    BigDecimal result = null;

    if (operator.equals("/")) {
        result = leftVal.divide(rightVal);
    } else if (operator.equals("x")) {
        result = leftVal.multiply(rightVal);

    } else if (operator.equals("+")) {
        result = leftVal.add(rightVal);
    } else if (operator.equals("-")) {
        result = leftVal.subtract(rightVal);

    }

    String resultStr = result+"";
    if (resultStr == null)
        return null;

    mOperationStack.clear();
    if (!requestedByUser) {
        mOperationStack.add(resultStr);
        mOperationStack.add(tmp);
    }

    return resultStr;
}

现在,如果我计算12/3输出是4,但如果我尝试计算12/5应用程序崩溃。每当除数不是分红应用程序的一个因素。阅读logcat后,它似乎是非终止分裂的原因。我怎么能避免它。这是我的logcat

03-09 22:33:14.876: W/dalvikvm(20200): threadid=1: thread exiting with uncaught exception (group=0x410819c0)
03-09 22:33:14.883: E/AndroidRuntime(20200): FATAL EXCEPTION: main
03-09 22:33:14.883: E/AndroidRuntime(20200): java.lang.IllegalStateException: Could not execute method of the activity
03-09 22:33:14.883: E/AndroidRuntime(20200):    at android.view.View$1.onClick(View.java:3607)
03-09 22:33:14.883: E/AndroidRuntime(20200):    at android.view.View.performClick(View.java:4212)
03-09 22:33:14.883: E/AndroidRuntime(20200):    at android.view.View$PerformClick.run(View.java:17476)
03-09 22:33:14.883: E/AndroidRuntime(20200):    at android.os.Handler.handleCallback(Handler.java:800)
03-09 22:33:14.883: E/AndroidRuntime(20200):    at android.os.Handler.dispatchMessage(Handler.java:100)
03-09 22:33:14.883: E/AndroidRuntime(20200):    at android.os.Looper.loop(Looper.java:194)
03-09 22:33:14.883: E/AndroidRuntime(20200):    at android.app.ActivityThread.main(ActivityThread.java:5371)
03-09 22:33:14.883: E/AndroidRuntime(20200):    at java.lang.reflect.Method.invokeNative(Native Method)
03-09 22:33:14.883: E/AndroidRuntime(20200):    at java.lang.reflect.Method.invoke(Method.java:525)
03-09 22:33:14.883: E/AndroidRuntime(20200):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-09 22:33:14.883: E/AndroidRuntime(20200):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-09 22:33:14.883: E/AndroidRuntime(20200):    at dalvik.system.NativeStart.main(Native Method)
03-09 22:33:14.883: E/AndroidRuntime(20200): Caused by: java.lang.reflect.InvocationTargetException
03-09 22:33:14.883: E/AndroidRuntime(20200):    at java.lang.reflect.Method.invokeNative(Native Method)
03-09 22:33:14.883: E/AndroidRuntime(20200):    at java.lang.reflect.Method.invoke(Method.java:525)
03-09 22:33:14.883: E/AndroidRuntime(20200):    at android.view.View$1.onClick(View.java:3602)
03-09 22:33:14.883: E/AndroidRuntime(20200):    ... 11 more
03-09 22:33:14.883: E/AndroidRuntime(20200): Caused by: java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result
03-09 22:33:14.883: E/AndroidRuntime(20200):    at java.math.BigDecimal.divide(BigDecimal.java:1266)
03-09 22:33:14.883: E/AndroidRuntime(20200):    at com.example.calculator.MainActivity.evaluateResult(MainActivity.java:239)
03-09 22:33:14.883: E/AndroidRuntime(20200):    at com.example.calculator.MainActivity.ProcessInput(MainActivity.java:149)
03-09 22:33:14.883: E/AndroidRuntime(20200):    ... 14 more

2 个答案:

答案 0 :(得分:2)

根据documentation BigDecimal,当结果为非终止小数时,将抛出ArithmeticException。您需要指定舍入模式:

leftVal.divide(rightVal,BigDecimal.HALF_UP);

如果要将其四舍五入到更多小数,可以在进行除法之前使用setScale(numDecimals)

答案 1 :(得分:1)

默认情况下,BigDecimal总是尝试返回操作的确切结果。

leftVal.divide(rightVal, 2, RoundingMode.HALF_UP);

其中2是精度,RoundingMode.HALF_UP是舍入模式