Spring 百里香中的不可靠行为';s th:选定属性
我有一个项目使用SpringBoot和Thymeleaf来呈现html页面。在其中一个页面中,我使用以下html让Thymeleaf选择一个选项:Spring 百里香中的不可靠行为';s th:选定属性,spring,spring-boot,thymeleaf,Spring,Spring Boot,Thymeleaf,我有一个项目使用SpringBoot和Thymeleaf来呈现html页面。在其中一个页面中,我使用以下html让Thymeleaf选择一个选项: <select name="value" id="usersWarning"> <option value="0" th:text="#{button.disabled}">0</option> <option value="0.5" th:selected="${warning} == 0.
<select name="value" id="usersWarning">
<option value="0" th:text="#{button.disabled}">0</option>
<option value="0.5" th:selected="${warning} == 0.5">50%</option>
<option value="0.75" th:selected="${warning} == 0.75">75%</option>
<option value="0.9" th:selected="${warning} == 0.9">90%</option>
<option value="0.95" th:selected="${warning} == 0.95">95%</option>
</select>
但在每种情况下,Thymeleaf正确显示为0.9或0.95
谢谢你的帮助。在过去的一个小时里,这让我抓狂。我建议尝试一下
${#numbers.formatDecimal(warning, 0, 2) == '0.95'}
这应该将数字格式化为带有两个十进制数字的字符串,允许您对结果执行字符串比较
这可能是必要的,因为浮点比较可能有非常小的舍入误差,从而导致严格比较失败。格式化为字符串会将数字舍入到较少的小数位数,并消除可能导致比较失败的小错误。这可能是因为浮点比较中存在舍入错误。您可以尝试将警告值四舍五入为两位数,并将其作为字符串进行比较,或者确定是否可以在表达式中使用Apache Commons Precision.compareTo()。@Kylos感谢您的建议。您是对的,尽管我使用value.compareTo(warning)==0进行了修复。
${#numbers.formatDecimal(warning, 0, 2) == '0.95'}