Android5.0 計算機計算結果不準確和結果末尾的多餘的‘0’沒有省略的解決方案

來源:互聯網
上載者:User

標籤:calculator   電腦計算結果不正確   計算機多餘的0沒有省略   

Android5.0 計算機計算結果不準確和結果末尾的多餘的‘0’沒有省略

 

一、問題的描述:

【測試步驟】

1.進入計算機

2.輸入 100-99.9

3.查看計算結果

【測試結果】

1.結果為: 0.0999999999

【預期結果】

1.結果應為: 0.1

 

 

【測試步驟】

1.進入計算機

2.輸入1000-999.9,查看結果是否為: 0.1

【測試結果】

1.結果顯示為: 0.10000000

【預期結果】

1.計算結果小數點後多餘的零應該省略。

 

二、解決方案:path:

 

<span style="font-size:18px;">package com.android.calculator2; <span style="color:#ff0000;">import java.util.Locale;//added </span>import org.javia.arity.Symbols;import org.javia.arity.SyntaxException;import org.javia.arity.Util; public class CalculatorExpressionEvaluator {     private static final int MAX_DIGITS = 12;    private static final int ROUNDING_DIGITS = 2;     private final Symbols mSymbols;    private final CalculatorExpressionTokenizer mTokenizer;     <span style="color:#ff0000;">private static final String NAN      = "NaN";//added</span>    public CalculatorExpressionEvaluator(CalculatorExpressionTokenizer tokenizer) {        mSymbols = new Symbols();        mTokenizer = tokenizer;    }     public void evaluate(CharSequence expr, EvaluateCallback callback) {        evaluate(expr.toString(), callback);    }     public void evaluate(String expr, EvaluateCallback callback) {        expr = mTokenizer.getNormalizedExpression(expr);         // remove any trailing operators        while (expr.length() > 0 && "+-/*".indexOf(expr.charAt(expr.length() - 1)) != -1) {            expr = expr.substring(0, expr.length() - 1);        }         try {            if (expr.length() == 0 || Double.valueOf(expr) != null) {                callback.onEvaluate(expr, null, Calculator.INVALID_RES_ID);                return;            }        } catch (NumberFormatException e) {            // expr is not a simple number        }         try {            double result = mSymbols.eval(expr);            if (Double.isNaN(result)) {                callback.onEvaluate(expr, null, R.string.error_nan);            } else {                // The arity library uses floating point arithmetic when evaluating the expression                // leading to precision errors in the result. The method doubleToString hides these                // errors; rounding the result by dropping N digits of precision.            <span style="color:#ff0000;">/*add*/            String strResult = "";                   for (int precision = MAX_DIGITS; precision > 6; precision--) {                strResult = tryFormattingWithPrecision(result, precision);                    if (strResult.length() <= MAX_DIGITS) {                        break;                    }                }                /*end*/</span>              <span style="color:#ff0000;">  final String resultString = mTokenizer.getLocalizedExpression(                        /*Util.doubleToString(result, MAX_DIGITS, ROUNDING_DIGITS)*/strResult);//modified</span>                callback.onEvaluate(expr, resultString, Calculator.INVALID_RES_ID);            }        } catch (SyntaxException e) {            callback.onEvaluate(expr, null, R.string.error_syntax);        }    }     public interface EvaluateCallback {        public void onEvaluate(String expr, String result, int errorResourceId);    }</span>
<span style="font-size:18px;"><span style="color:#ff0000;">    /*added*/     private String tryFormattingWithPrecision(double value, int precision) {        // The standard scientific formatter is basically what we need. We will        // start with what it produces and then massage it a bit.        String result = String.format(Locale.US, "%" + MAX_DIGITS + "." + precision + "g", value);        if (result.equals(NAN)) { // treat NaN as Error            return "NAN";        }                String mantissa = result;        String exponent = null;        int e = result.indexOf('e');        if (e != -1) {            mantissa = result.substring(0, e);             // Strip "+" and unnecessary 0's from the exponent            exponent = result.substring(e + 1);            if (exponent.startsWith("+")) {                exponent = exponent.substring(1);            }            exponent = String.valueOf(Integer.parseInt(exponent));        } else {            mantissa = result;        }         int period = mantissa.indexOf('.');        if (period == -1) {            period = mantissa.indexOf(',');        }        if (period != -1) {            // Strip trailing 0's            while (mantissa.length() > 0 && mantissa.endsWith("0")) {                mantissa = mantissa.substring(0, mantissa.length() - 1);            }            if (mantissa.length() == period + 1) {                mantissa = mantissa.substring(0, mantissa.length() - 1);            }        }         if (exponent != null) {            result = mantissa + 'e' + exponent;        } else {            result = mantissa;        }        return result;    }    /*end*/</span>}</span>


 

 

 

Android5.0 計算機計算結果不準確和結果末尾的多餘的‘0’沒有省略的解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.