在下列代码中 th:value="${#numbers.formatDecimal(commission.serviceFeeRate * 100 , 2, '0')}" 判断如果commission.serviceFeeRate内容为空,则给0
时间: 2024-02-17 07:04:47 浏览: 17
可以使用 Elvis 运算符(`?:`)来实现这个功能,Elvis 运算符在判断左侧表达式是否为 `null` 或 `undefined`,如果是,则返回右侧表达式的值。
以下是修改后的代码示例:
```html
<th:block th:with="serviceFeeRate=${commission.serviceFeeRate ?: 0}">
<td th:text="${#numbers.formatDecimal(serviceFeeRate * 100, 2, '0')}" />
</th:block>
```
在这个示例中,我们使用了 `th:with` 来定义一个新的变量 `serviceFeeRate`,并为其赋值为 `commission.serviceFeeRate` 的值,如果 `commission.serviceFeeRate` 为空,则赋值为 `0`。然后在 `<td>` 元素中使用 `serviceFeeRate` 变量来计算并显示格式化后的数值。
注意,这里的 Elvis 运算符 `?:` 只能判断左侧表达式是否为 `null` 或 `undefined`,如果左侧表达式是空字符串 `''`,则不会触发 Elvis 运算符,仍然会显示空字符串。如果需要判断空字符串,可以使用 `or` 运算符 `||` 或者 `#strings` 工具类的 `isBlank` 方法来实现。
相关问题
<input id="serviceFeeRateOutput" th:value="${#numbers.formatDecimal(commission.serviceFeeRate * 100 , 2, '0')}" th:placeholder="#{html.commission.msg.serviceFeeRate}" class="form-control" type="number" maxlength="30" required> 报如下错误 Exception evaluating SpringEL expression: "#numbers.formatDecimal(commission.commissionRateS * 100 , 2, '0')" (template: "main/web/commission/commission/edit" - line 51, col 63)
这个错误通常是因为 `commission.serviceFeeRate` 的值为 `null` 或 `undefined` 导致的。可以在表达式中使用 `?:` 运算符来判断 `commission.serviceFeeRate` 是否为空,如果为空则使用默认值 `0`。
以下是修改后的代码示例:
```html
<input id="serviceFeeRateOutput"
th:value="${commission.serviceFeeRate != null ? #numbers.formatDecimal(commission.serviceFeeRate * 100 , 2, '0') : '0'}"
th:placeholder="#{html.commission.msg.serviceFeeRate}"
class="form-control"
type="number"
maxlength="30"
required>
```
在这个示例中,我们使用了三目运算符 `condition ? valueIfTrue : valueIfFalse` 来判断 `commission.serviceFeeRate` 是否为空。如果不为空,则使用 `#numbers.formatDecimal(commission.serviceFeeRate * 100 , 2, '0')` 计算格式化后的数值;否则使用字符串 `'0'` 作为默认值。
另外,如果 `commission.serviceFeeRate` 可能为字符串类型的数字,可以先使用 `#strings.trim()` 和 `#strings.isEmpty()` 方法来判断其是否为空。例如:
```html
<input id="serviceFeeRateOutput"
th:value="${#strings.trim(commission.serviceFeeRate) != '' ? #numbers.formatDecimal(commission.serviceFeeRate * 100 , 2, '0') : '0'}"
th:placeholder="#{html.commission.msg.serviceFeeRate}"
class="form-control"
type="number"
maxlength="30"
required>
```
这样可以先将字符串类型的数字去掉空格,再判断是否为空。
C语言程序:计算股票经纪人的佣金 当股票通过经纪人进行买卖时,经纪人的佣金往往根据股票交易额采用某种变化的比例进行计算。下面的表格显示了实际支付给经纪人的费用的数量: 交易额范围 佣金费用 低于2500美元 30美元+1.7% 2500~6250美元 56美元+0.66% 6250~20000美元 76美元+0.34% 20000~50000美元 100美元+0.22% 50000~500000美元 155美元+0.11% 超过500000美元 255美元+0.09% 最低收费是39美元。下面的程序要求用户输入交易额,然后显示出佣金的数额: Enter value of trade: 30000 Commission: $166.00 **输入格式要求:"%f" 提示信息:"Enter value of trade:" **输出格式要求:"Commission: $%.2f\n"
以下是计算股票经纪人佣金的C语言代码实现:
```c
#include <stdio.h>
int main() {
float value, commission;
printf("Enter value of trade: ");
scanf("%f", &value);
if (value < 2500) {
commission = 30 + value * 0.017;
} else if (value < 6250) {
commission = 56 + value * 0.0066;
} else if (value < 20000) {
commission = 76 + value * 0.0034;
} else if (value < 50000) {
commission = 100 + value * 0.0022;
} else if (value < 500000) {
commission = 155 + value * 0.0011;
} else {
commission = 255 + value * 0.0009;
}
if (commission < 39) {
commission = 39;
}
printf("Commission: $%.2f\n", commission);
return 0;
}
```
在上面的代码中,我们使用 if-else 语句根据交易额范围计算佣金,并将结果存储在 `commission` 变量中。由于最低收费是39美元,因此如果计算出来的佣金小于39美元,则将其设为39美元。最后,我们使用 `%f` 格式字符串输出佣金结果。注意,由于需要保留两位小数,因此我们在输出时使用了 `%.2f` 格式字符串。