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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7582265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:35:13  来源:igfitidea点击:

How to convert String to double in JSP

javajsp

提问by London

I'm extracting some results in the table in JPS like this :

我正在像这样在 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>

Now I'd like to add one more column which will be percentage ${item[2]}divided by ${item[1]}. But these are strings how do convert the result to double?

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

EDIT:

编辑:

In both answers so far I get compile error resulting in :

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

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>

More code :

更多代码:

<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>

回答by adatapost

Don't worry about type conversion If an expressions ${item[2]}or ${item[1]}contains numeric value (digits). Simply use,

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

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

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

or

或者

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

From EL Specification:

来自 EL 规范:

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.

每个表达式都在预期类型的​​上下文中进行评估。表达式计算的结果可能与预期类型不完全匹配,然后将按照强制规则进行转换。

Edit:

编辑:

For better design try to change datasource but do not add scriptlets. You can use entity (bean) list.

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

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

回答by Simeon

<td><%= Double.valueOf(item[2]) / Double.valueOf(item[1]) %></td>

回答by claymore1977

I'd setup the values at the top of the JSP in a <% %> or in the controller (servlet) itself:

我会在 JSP 顶部的 <% %> 或控制器(servlet)本身中设置值:

    String[] item = new String[1]; /*  Assume a standard array */

    String sOne = null, sTwo = null;
    double dOne = 0.0d, dTwo = 0.0d, dPercent;

    try {
        sOne = item[1];
        sTwo = item[2];

        dOne = Double.parseDouble(item[1]);
        dTwo = Double.parseDouble(item[2]);

        /* Making an assumption on the *100 here.... */
        dPercent = (dTwo/dOne)*100; 

    } catch(NumberFormatException nfe) {
        /*  Handle the NFE here...*/
    } catch (NullPointerException npe) {
        /*  Handle the NPE here...*/
    } catch (Exception e) {
        /*  Handle any other exception here...*/
    }

Preprocessing the data at the top of the JSP or in the controller keeps the JSP cleaner:

在 JSP 顶部或控制器中预处理数据使 JSP 更干净:

<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>
  <td><%=dPercent%></td>
</tr>

Note that the difference between Double.parseDouble() and Double.valueOf() is the return type. .valueOf() returns a Double object while .parseDouble() returns a double type.

请注意, Double.parseDouble() 和 Double.valueOf() 之间的区别在于返回类型。.valueOf() 返回一个 Double 对象,而 .parseDouble() 返回一个 double 类型。

Some java programmers use 'double' and Double interchangeably, but I come from the camp of 'avoid the Objects, use the primitive types!'. Not only does Double use more memory than a double does, but not knowing the difference between the two can cause some (major) confusion. See this link for an int vs Integer example: http://illegalargumentexception.blogspot.com/2008/08/java-int-versus-integer.html

一些java程序员交替使用'double'和Double,但我来自'避免对象,使用原始类型!'的阵营。Double 不仅比 double 使用更多的内存,而且不知道两者之间的区别会导致一些(主要)混淆。请参阅此链接以获取 int 与 Integer 示例:http: //illegalargumentexception.blogspot.com/2008/08/java-int-versus-integer.html