如何将带有操作数符号的字符串转换为double?

时间:2017-11-15 23:15:53

标签: java string math double

在java中,以下代码有效:

    Dim j As Integer
    Dim MidPointE As String
    Dim Dist As String
    Dim Allocation As String

    MidPointE = "AM"
    Dist = "AN"
    Allocation = "AO"

   for J= 1 to 300
     Cells(j, Dist).Formula = "=IFERROR(" & MidPointE & j & " / " & MidPointE &  CustomerLast & ", "")"
    Cells(j, Allocation).Formula = "=" & Allocation & j & "* S" & CustomerLast

     Next J

这将打印double a = 15.5+0.5; System.out.println(a);

那么为什么以下内容会返回运行时错误:

16.0

如何将第二个示例变为不给出错误,并且在将字符串转换为double并计算值时表现得像第一个?

1 个答案:

答案 0 :(得分:0)

parseDoubleparseInteger等不执行表达式评估,他们只是执行从某些值的字符串表示到数字表示的转换(所以{ {1}}转换为"5.0")。

请参阅文档here,了解5.0行为的准确定义。

如果要执行表达式评估,可以查看反向波兰表示法和分流场算法,或理想情况下library designed for this。或者,您可以使用a more "hacky" solution