【发布时间】:2019-10-16 17:12:32
【问题描述】:
我在 Thymeleaf 中遇到数字格式问题。 我有一个 html 模板,它通过遍历每个账单来显示账单数据。 大多数字段是 String 类型,但 brokerageRate 字段在我的数据模型中是 Double 类型。 当我运行我的代码时,它给了我以下异常 评估 SpringEL 表达式的异常:“#numbers.formatDecimal(bill.brokerageRate,3,2)”
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
我尝试了 Thymeleaf 文档中提供的不同选项,但我得到了同样的例外。 https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html
以下是我的模板:-
<tbody>
<tr th:each="bill : ${bills}">
<td><a th:text="${bill.invoiceNum}" th:href="@{'/show/' + ${bill.id}}"></a></td>
<td th:text="${bill.invoiceDate}"></td>
<td th:text="${bill.receiverName}"></td>
<td th:text="${bill.receiverAddress}"></td>
<td th:text="${bill.receiverState}"></td>
<td th:text="${bill.receiverGstNum}"></td>
<td th:text="${bill.propertyAddress}"></td>
<td th:text="${#numbers.formatDecimal(bill.brokerageRate,3,2)}"></td>
<td th:text="${bill.brokerageAmount}"></td>
<td th:text="${bill.cgstPct}"></td>
<td th:text="${bill.cgstAmount}"></td>
<td th:text="${bill.sgstPct}"></td>
<td th:text="${bill.sgstAmount}"></td>
<td th:text="${bill.igstPct}"></td>
<td th:text="${bill.igstAmount}"></td>
<td th:text="${bill.totalGstAmount}"></td>
<td th:text="${bill.brokerageDueAfterTax}"></td>
</tr>
</tbody>
请求您帮助我理解我哪里错了。
【问题讨论】:
-
整个堆栈跟踪是什么——它应该告诉你问题是什么?如果我不得不猜测,我会猜想有时
bill.brokerageRate为空,因为表达式在我看来没问题。 -
注意事项 - 对于需要精度的值,您可能希望使用 BigDecimal 而不是 Double
-
问题已解决。我使用 ${bill.brokerageRate} 而不是使用 numbers.format 十进制语法,效果很好
标签: html spring-boot thymeleaf