[英]Calculation with <s:property /> values in Struts2
我有以下代碼:
<s:property value="currentPrice" />
<s:property value="oldPrice" />
<span class="badge">-50%</span>
現在,我想通過公式在<span>
標記中顯示百分比:
百分比=(當前價格-舊價格)/舊價格* 100
如何在Struts2中做到這一點?
客戶端解決方案:
<s:property value="currentPrice" />
<s:property value="oldPrice" />
<span class="badge">
<s:property value="%{((currentPrice - oldPrice) / oldPrice) * 100}" />%
</span>
或者,
服務器端解決方案:
public Integer getPercentage(){
return ((currentPrice - oldPrice) / oldPrice) * 100;
}
<s:property value="currentPrice" />
<s:property value="oldPrice" />
<span class="badge">
<s:property value="percentage" />%
</span>
您可以使用javascript如下所示-
var currentPrice = document.getElementById("currentPrice").value;
var oldPrice = document.getElementById("oldPrice").value;
var percentage = (currentPrice-oldPrice)/oldPrice * 100
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.