Shyju
Shyju

Reputation: 218852

VB script math function

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!

Upvotes: 0

Views: 1216

Answers (4)

Justin Niessner
Justin Niessner

Reputation: 245459

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.

Upvotes: 3

1800 INFORMATION
1800 INFORMATION

Reputation: 135395

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.

Upvotes: 0

Guffa
Guffa

Reputation: 700592

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

Upvotes: 1

Kevin Tighe
Kevin Tighe

Reputation: 21181

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

Upvotes: 0

Related Questions