[英]abs not working on lucee coldfusion
我正在使用以下格式來格式化在計算百分比增加或減少后得到的數字,這意味着該數字可以是正數或負數。 下面的代碼在Coldfusion上工作正常,但是在Lucee上,它拋出錯誤-無法將[-6.50]字符串轉換為數字值。 任何想法如何解決此問題。
<cfif money_deposit lt 0>
<cfset testVar = abs(NumberFormat(money_deposit,'99.99'))>
<cfelse>
<cfset testVar = NumberFormat(money_deposit,'99.99')>
</cfif>
您應該能夠使用javaCast
<cfif money_deposit lt 0>
<cfset testVar = abs(NumberFormat(javaCast("float", money_deposit),'99.99'))>
<cfelse>
<cfset testVar = NumberFormat(money_deposit,'99.99')>
</cfif>
首先,刪除所有空白區域。 無論如何,最好先測試輸入參數是否為數字。
<!--- Remove any spaces --->
<cfset money_deposit = REreplace(money_deposit,"\s","","all")>
<cfif isNumeric(money_deposit)>
<cfif money_deposit lt 0>
<cfset testVar = abs(NumberFormat(money_deposit,'-99.99'))>
<cfelse>
<cfset testVar = NumberFormat(money_deposit,'99.99')>
</cfif>
</cfif>
數字格式返回字符串http://docs.lucee.org/reference/functions/numberformat.html
問題是格式掩碼“ 99.99”表示返回一個帶小數點前兩個字符的格式化數字,返回的字符串值為“-6.50”,並帶有多余的空格。 嘗試使用數字格式為“ 9.99”或“ 09.99”的掩碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.