gpt4 book ai didi

java - 如何在JSP中将String转换为double

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:15:23 24 4
gpt4 key购买 nike

我在 JPS 中的表格中提取一些结果,如下所示:

<tr>
<td>${item[0]}</td>
<td>${item[5]}</td>
<td>${item[1]}</td>
<td>${item[2]}</td>
<td>${item[5]}</td>
<td>${item[6]}</td>
</tr>

现在我想再添加一列,它将百分比 ${item[2]} 除以 ${item[1]} 。但是这些是字符串如何将结果转换为 double ?

编辑:

到目前为止,在这两个答案中我都得到编译错误导致:

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 30 in the jsp file: /WEB-INF/views/home.jsp
Syntax error, insert ")" to complete MethodInvocation
27: <td>${item[5]}</td>
28: <td>${item[1]}</td>
29: <td>${item[2]}</td>
30: <td><%= Double.parseDouble(${item[2]})/Double.parseDouble(${item[1]}) %> </td>
31: <td>${item[5]}</td>
32: <td>${item[6]}</td>
33: </tr>

更多代码:

<table width="100%">
<c:forEach var="item" items="${myList}">
<tr>
<td>${item[0]}</td>
<td>${item[5]}</td>
<td>${item[1]}</td>
<td>${item[2]}</td>
<td><%= Double.valueOf(item[2]) / Double.valueOf(item[1]) %></td>
<td>${item[5]}</td>
<td>${item[6]}</td>
</tr>
</c:forEach>
</table>

最佳答案

如果表达式 ${item[2]}${item[1]} 包含数值(数字),不用担心类型转换。简单使用,

${item[2]/item[1]}

<c:catch>
${items[0]/items[1]}
</c:catch>

From EL Specification:

Every expression is evaluated in the context of an expected type. The result of the expression evaluation may not match the expected type exactly, then the conversion will be take place as per coercion rules.

编辑:

为了更好的设计,请尝试更改数据源但不要添加 scriptlet。您可以使用实体(bean)列表。

${item.produtid}
${item.qty*item.rate}

关于java - 如何在JSP中将String转换为double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7582265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com