views:

446

answers:

4

Hi friends,

I have an ASP page where I have 2 variables, strActualRate and strProposed. The values are:

strActualRate = 33.30
strProposed = 33.3

So when I write the following line to compare:

if strActualRate <> strProposed  then
  Response.Writr "Both are not equal!"
end if

I am getting the output "Both are not equal", even though both are the same. I am sure that I need to use some mathematical conversion function to compare.

Can anyone tell me how to solve this ?

Thanks in advance!

+3  A: 

If I understand correctly, you think the two values are equal but because VBScript is comparing strings rather than numbers the two are coming back as not equal.

You're correct in the conversion idea, and here's the code:

if CDbl(strActualRate) <> CDbl(strProposed) then
     Response.Write "Both are not equal!"
end if

That will convert your string values to numbers to do the comparison.

Justin Niessner
If the values are in fact strings (contrary to what the code shows), that would convert both values to 33 before comparing them. If the values were for example "33.1" and "33.4" they would also end up being equal.
Guffa
No, doubles have a decimal part. `CDbl()` would leave them as 33.3. `CInt()` would convert them to 33.
Dave DuPlantis
A: 

Try casting the values to a double in the comparison statement with CDbl()

Kevin Tighe
+1  A: 

Your question doesn't really add up, so I'm not really sure what the problem is. I will try to clear up some things about data types and comparison.

You are using the prefix "str" for your variables which suggests that you intend to store string values in them, however you are instead storing numeric values in them. Either you are confused about how hungarian notation is used to keep track of the data type, or the code that you posted does not look like the code that you are actually using.

The numeric value 33.30 is exactly the same as the value 33.3. If you instead would have used the string values "33.30" and "33.3", they would be two strings that are not equal.

If your code is corrected (Response.Write instead of Response.Writr) so that it runs, it will not produce any output at all. As the values are equal, the condifion in the if statement evaluates to false.

If you do in fact assign string values to the variables, the code would output "Both are not equal!". This is just as expected as the strings are not equal. If you have strings and want to compare them as numerical values, you have to comvert them:

If CDbl(strActualRate) <> CDbl(strProposed)  Then
  Response.Write "Both are not equal!"
End If
Guffa
Thanks Guffa,Ya It was some typing mistake for me when i posted here. Actually i was just looking for the Conversion method
Shyju
A: 

Are you intending to perform the comparison as strings, floating point numbers or some other method? If you are comparing them as strings, then clearly they are not equal, as one of them has an extra zero on the end. If you are comparing them as floating point numbers, then you generally want to use a comparison that involves taking the difference and checking that it is smaller than some small value. This is because floating point number calculations involve some degree of inaccuracy and comparisons between them can fail because of the underlying representation.

1800 INFORMATION