gpt4 book ai didi

java - Thymeleaf 中的百分比计算 - 结果为零

转载 作者:行者123 更新时间:2023-11-30 01:46:09 25 4
gpt4 key购买 nike

我们有一个java对象:

class Course{

long totalStudens;
long graduatedStudents;
}

Thymeleaf 页面中,我们希望显示 graduatedStudentstotalStudens 中所占的百分比

所以我们做了类似的事情,但我们继续在结果中得到 0,即使结果不应该为零:

<div th:with="result=${#numbers.formatDecimal((course.graduatedStudents/course.totalStudens)*100, 1, 'POINT', 2, 'COMMA')}">
<td th:text="${result}"></td>
</div>

我们还尝试了一些更简单的方法:

 <td th:text="${#numbers.formatDecimal(course.graduatedStudents/course.totalStudends, 1, 'POINT', 2, 'COMMA')}"></td>

这与longdouble问题有关吗?如果是的话,如何解决?我们更喜欢在 Thymeleaf 端执行此操作,而不是在 java

最佳答案

由于您正在进行long/long除法,因此结果将为long。如果您想接收浮点精度结果,则必须将其中一个因子转换为 double,例如将其乘以 1.0:

<div th:with="result=${#numbers.formatDecimal((1.0 * course.graduatedStudents / course.totalStudens)*100, 1, 'POINT', 2, 'COMMA')}">
<td th:text="${result}"></td>
</div>

关于java - Thymeleaf 中的百分比计算 - 结果为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57864887/

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