Skip to main content
Inspiring
October 15, 2008
Answered

Value cannot be converted to a number

  • October 15, 2008
  • 2 replies
  • 3499 views
I can't figure out why this will not add. I have a MySQL database with the fields "postage" and "copies" set as varchar.

copies is 29.30 and postage is 37.22. I just trying to add things using the following code:

<cfset postage = "<cfoutput>#qcases.postage#</cfoutput>">
<cfset copies = "<cfoutput>#qcases.copies#</cfoutput>">

<cfset total = total + postage + copies>

I get the error The value "37.22" cannot be converted to a number

I have tried using these methods but no luck:

<cfset total = total + #LsNumberFormat(postage)# + #LsNumberFormat(copies)#>

and

<cfset total = total + #NumberFormat(postage)# + #NumberFormat(copies)#>

and

<cfset total = total + #DecimalFormat(postage)# + #DecimalFormat(copies)#>


    This topic has been closed for replies.
    Correct answer JR__Bob__Dobbs-qSBHQ2
    It may be that you're trying to add two strings rather than two numbers

    <cfoutput>
    <cfset postage = #qcases.postage#>
    <cfset copies = #qcases.copies#>
    </cfoutput>

    In fact - before you do this try taking the ) out after copies - #qcases.copies)#

    Michael

    <cfset postage = #qcases.postage#>
    <cfset copies = #qcases.copies#>

    <cfset total = Val(total) + Val(postage) + Val(copies)>

    Note that the Val function will convert its argument to a number or return 0 for a non-numeric argument

    http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_t-z_12.html#139109

    2 replies

    Inspiring
    October 16, 2008
    Hi,
    Here are two more you can try:
    Instead of LsNumberFormat you should use LSParseNumber.
    (Depending on the local setting of your server this may make a difference)

    Use trim to clean-up the values:
    <cfset total=trim(val1) * trim(val2)>
    (The text representation of your numbers may include a leading space, placeholder for the sign +/-)

    cheers,
    fober




    Inspiring
    October 15, 2008
    Try:

    <cfset total = Evaluate(total + postage + copies)>

    The Evaluate function will treat the strings as numbers.
    brianismAuthor
    Inspiring
    October 15, 2008
    Thanks for the quick response. I tried your method but I still get the same error.
    Known Participant
    October 15, 2008
    It may be that you're trying to add two strings rather than two numbers

    <cfoutput>
    <cfset postage = #qcases.postage#>
    <cfset copies = #qcases.copies#>
    </cfoutput>

    In fact - before you do this try taking the ) out after copies - #qcases.copies)#

    Michael