Java方法计算("2.002*1000")//返回2001.999999999999

use*_*096 0 java evaluation

我已经使用方法calculate(String)来计算String类型中的操作数,例如"2 + 3".我已经尝试了很多操作数,方法工作正常,除了这个:calculate("2.002*1000)//返回2001.9999999999我很好奇怎么会发生这个错误.我已经多次查看代码而且我不能找出问题所在.

这里的代码:

private String calculate(String str)
    {
        String num = "";
        List<String> list = new ArrayList<String>();
        for(int i = 0; i < str.length(); i++) //Arrange str into list, elements are seperated by "+" or "-"
        {
            if(! checkPM(str.substring(i, i + 1)))
            {
                num = num + str.substring(i, i + 1);
            } else
            {
                list.add(num);
                list.add(str.substring(i, i + 1));
                num = "";
            }
        }
        list.add(num);//add the last num into list
        if(checkSign(list.get(list.size() - 1)))//remove last element if it is an operator
        {
        list.remove(list.get(list.size() - 1));
        }
        String numlistele = ""; //Elements of numlist
        List<String> numlist = new ArrayList<String>(); //List of numbers to be TD
        List<String> TDlist = new ArrayList<String>(); //List of times or divide
        for(int j = 0; j < list.size(); j++) //Check which of the elements of list contains "*" or "/"
        {
            String tdAns = ""; //Answer of numlistele timed or divided
            if(checkTD(list.get(j))) // When the elements of list contains "*" or "/"
            {
                for(int k = 0; k < list.get(j).length(); k++) //
                {
                    if(! checkSign(list.get(j).substring(k, k + 1)))
                    {
                        numlistele = numlistele + list.get(j).substring(k, k+1);
                    } else
                    {
                        numlist.add(numlistele);
                        TDlist.add(list.get(j).substring(k, k+1));
                        numlistele = "";
                    }
                }
                numlist.add(numlistele); //Adds the last number into numlist
                numlistele = ""; //Restore numlistele to "", to be used in next loop 
                tdAns = numlist.get(0); //Answer of numlistele timed or divided, firstly it is equals to the first elements of numlist
                for(int l = 0; l < TDlist.size(); l++)
                {
                    if(TDlist.get(l).equals("×"))
                    {
                        double tempdou = Double.parseDouble(tdAns) * //temporary double used to save to tdANS
                                Double.parseDouble(numlist.get(l+1));
                        tdAns = String.valueOf(tempdou);
                    } else //when TDlist.get(l).equals("/") is true
                    {
                        double tempdou = Double.parseDouble(tdAns) / 
                                Double.parseDouble(numlist.get(l+1));
                        tdAns = String.valueOf(tempdou);
                    }
                }
            list.set(j, tdAns);
            }               
            numlist.clear(); //Clear numlist for next loop
            TDlist.clear(); //Clear TDlist for next loop
        }
        String ans = list.get(0); //Will become final answer later, first it is assign to first element of list
        for(int m = 0; m < list.size(); m++)
        {
            if(list.get(m).equals("+"))
            {
                double tempdou = Double.parseDouble(ans) + //Temporary double used to save to ans
                        Double.parseDouble(list.get(m + 1));
                ans = String.valueOf(tempdou);
            } else if(list.get(m).equals("-"))
            {
                double tempdou = Double.parseDouble(ans) - 
                        Double.parseDouble(list.get(m + 1));
                ans = String.valueOf(tempdou);
            }
        }
        if(ans.length() > 2)
        {
            if(ans.substring(ans.length() - 2).equals(".0")) //To remove .0 of the answer
            {
                ans = ans.substring(0, ans.length() - 2);
            }

        }
        return ans;
    }
Run Code Online (Sandbox Code Playgroud)

提前致谢.

rge*_*man 6

浮点表示并不总是那么准确.解析字符串"2.002"后,Double.parseDouble返回最接近的double表示形式.

乘以时1000,浮点表示中的误差变得足够大以至于注意到(2001.999999999999).

这是一篇关于浮点表示和可能发生的不幸事件的在线文章.

要确保准确表示浮点数,可以使用Java的内置BigDecimal类.它可能不会那么快,但它会保留您所需的所有精度.