Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Value cannot be converted to a number

Participant ,
Oct 15, 2008 Oct 15, 2008
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)#>


3.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advisor , Oct 15, 2008 Oct 15, 2008
<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
Translate
Explorer ,
Oct 15, 2008 Oct 15, 2008
Try:

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

The Evaluate function will treat the strings as numbers.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 15, 2008 Oct 15, 2008
Thanks for the quick response. I tried your method but I still get the same error.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 15, 2008 Oct 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Oct 15, 2008 Oct 15, 2008
<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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 15, 2008 Oct 15, 2008
LATEST
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




Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources