1
votes

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>
3
If money_forecast is numeric, try switching the numberFormat and abs functions around. - Dan Bracuk
Its not numeric - user747291

3 Answers

3
votes

You should be able to use 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>
1
votes

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>
0
votes

Number format returns a string http://docs.lucee.org/reference/functions/numberformat.html

The problem is your formatting mask "99.99" means return a formatted number with two characters before the decimal point, the string value you are getting back is "- 6.50", with an extra space. Try using a mask for numberformat of either "9.99" or "09.99"