Domanda

Afternoon,

Im playing around with a little bidding script im trying to write. But im having trouble with formatNumber function.

currentBid = 50.51 'from database dataType double(16,2)
yourBid = isNumeric(Request("bid"))

If FormatNumber(yourBid,2) > FormatNumber(currentBid,2) Then
  Response.Write"bid successful... woop woop"
      else
  Response.Write"you cant bid below the current asking price"
end if

But if i was to bid 1000 is writes "you cant bid below the current asking price"

Please advise

Regards
Shane

 'Changed as advised

 currentBid = 50.51 'value from database

 If IsNumeric(Request.Form("bid")) Then
 yourBid = CDbl(Request.Form("bid"))
 end if
È stato utile?

Soluzione

You have two issues here:

  1. As Ekkehard mentioned, IsNumeric() returns a boolean. To test if the value is numeric and then store to your variable, use:

    If IsNumeric(Request("bid")) Then yourBid = CDbl(Request("bid"))
    
  2. FormatNumber() returns a string representation of a number. So you're comparing one string against another instead of one number against another. If you need to round your numbers to two decimals, use the Round() function instead:

    If Round(yourBid,2) > Round(currentBid,2) Then
    

Edit: Proof.

MsgBox VarType(4)                ' 2 = vbInteger
MsgBox VarType(FormatNumber(4))  ' 8 = vbString

Altri suggerimenti

The line

yourBid = isNumeric(Request("bid"))

does not store a valid number into yourBid, but the (booelan) result of the IsNumeric() function applied to Request("bid").

Change the line to

yourBid = CDbl(Request("bid"))

and see if your IF statement works as expected. Then add a proper validation for Request("bid").

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top