开发者

The value cannot be converted to a number

Got a critical error like: The value cannot be converted to a number. what can be the problem? Since i tried to write this values like:

1. <cfset ortalama=trim(val(db_maliyet_temp))+开发者_开发百科ds_toplam_maliyet>

2. <cfset ortalama=val(db_maliyet_temp)+ds_toplam_maliyet>

3. <cfset ortalama=db_maliyet_temp+ds_toplam_maliyet>

the first and second are just doesnt count the db_maliyet_temp, and the 3 give out the error: The value cannot be converted to a number.


value for db_maliyet_temp: 2.806,71 for ds_toplam_maliyet: 394,22

These are not valid numbers. If you would like the total of the numbers, you can try this.

<cfset aryMaliyetNumbers = ListToArray(db_maliyet_temp, ",")>
<cfset aryToplamNumbers = ListToArray(ds_toplam_maliyet, ",")>
<cfset total = ArraySum(aryMaliyetNumbers) + ArraySum(aryToplamNumbers)>

There are several ways to skin this cat. This should at least get you going. Works perfectly on my CF 7 box!

EDIT

After the ridiculous amount of comments to clarify the question, I believe this is the solution.

<cfset db_maliyet_temp = Replace(Replace("2.806,71", ".", ""), ",", ".")>
<cfset ds_toplam_maliyet = Replace(Replace("394,22", ".", ""), ",", ".")>
<cfset total = db_maliyet_temp + ds_toplam_maliyet>

If you want the number without decimals, you can do this:

<cfset db_maliyet_temp = Replace(Replace("2.806,71", ".", ""), ",", ".")>
<cfset ds_toplam_maliyet = Replace(Replace("394,22", ".", ""), ",", ".")>
<cfset total = val(db_maliyet_temp + ds_toplam_maliyet)>

IMPORTANT

You have a much larger problem than a CF error. You need to fix the underlying issue that's causing your number to be formatted incorrectly.


This should do the trick:

<cfscript>
    function convertToNumber(num){
          return reReplace(reReplace(num,'.','','ALL'),',','.','ALL');
    }
</cfscript>

<cfset ortalama=convertToNumber(db_maliyet_temp)+convertToNumber(ds_toplam_maliyet)>

Basically it just removes the '.' since that is formatting not needed for math and replaces the ',' with a decimal so that it can be treated as a number. This will only work if ALL the numbers you are going to be dealing with are formatted this way, if there are any formatted like 1,200.90 then you will have to be a little more fancy.


Have you tried the LSParseNumber() function?:

http://cfquickdocs.com/cf9/#lsparsenumber

Or the val() function?:

http://cfquickdocs.com/cf9/#val

Also, it may be easier to clean the data before it get's entered either with client-side validation (if it's coming from a form) or server-side validation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜