Search code examples
coldfusionlucee

abs not working on lucee coldfusion


I am using the below to format the number I get after calulating the % increase or decrease, which means this number can be positive or a negative number. The below works fine on Coldfusion, but on Lucee it throws an error - can't cast [- 6.50] string to a number value. Any idea how to workaround this.

<cfif money_deposit lt 0>
    <cfset testVar = abs(NumberFormat(money_deposit,'99.99'))>
<cfelse>
    <cfset testVar = NumberFormat(money_deposit,'99.99')>
</cfif>

Solution

  • First, remove any empty spaces. In any case, it is good practice to first test whether the input parameter is numeric.

    <!--- 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>