We are currently having an issue due to implicit conversion in an IF statement in VBScript (Classic ASP) that don't do implicit conversion the same way when dealing with a variable or a literal. Can someone explain this behavior to me, why do VBScript acts this way ?
Here is a sample of what I mean :
Const c_test = 3
Dim iId : iId = 3
Dim iTestStr : iTestStr = "3"
If iId = iTestStr Then
Response.Write("Long variable = String variable : Equal")
Else
Response.Write("Long variable = String variable : Not Equal")
End If
Response.Write("<br/>")
If c_test = iTestStr Then
Response.Write("Long constant = String variable : Equal")
Else
Response.Write("Long constant = String variable : Not Equal")
End If
Response.Write("<br/>")
If c_test = iId Then
Response.Write("Long constant = Long variable : Equal")
Else
Response.Write("Long constant = Long variable : Not Equal")
End If
Response.Write("<br/>")
If iId = "3" Then
Response.Write("Long variable = String literal : Equal")
Else
Response.Write("Long variable = String literal : Not Equal")
End If
Response.Write("<br/>")
If c_test = "3" Then
Response.Write("Long constant = String literal : Equal")
Else
Response.Write("Long constant = String literal : Not Equal")
End If
Which ouputs :
Long variable = String variable : Not Equal
Long constant = String variable : Not Equal
Long constant = Long variable : Equal
Long variable = String literal : Equal
Long constant = String literal : Equal
Which is quite confusing o_O
This is the result of one documented behavior and one undocumented one.
The documented behavior is that in comparisons, a number is always less than a string. This is mentioned in the documentation for Comparison Operators. Paraphrasing the table near the bottom of the page:
If one expression is numeric and the other is a string, then the numeric expression is less than the string expression.
The undocumented behavior is that comparisons involving literals are handled differently from comparisons involving variables. See this blog entry for more details. To summarize the important conclusion:
The relevant comparison rules in VB6/VBScript go like this:
Hard string ~ hard number: convert string to number, compare numbers
Hard string ~ soft number: convert number to string, compare strings
Soft string ~ hard number: convert string to number, compare numbers
Soft string ~ soft number: any string is greater than any number
The documented behavior explains why the first two comparisons are false, while the undocumented behavior explains why the last two comparisons are true.
You are (implicitly) declaring your variables As Variant so your If conditions actually test the equality of two Variants and determine that they are unequal.
In the last cases, however, you are using String constants (which can never be Variant, even if declared without a type) and String literals.
My guess is that when you compare two Variants, VB first determines whether they have the same type tag and if they don’t, resolves to False.
Related
using vb asp.net
error: Problem with database (2). Conversion from string "Price" to type 'Integer' is not valid.
Dim testP As Decimal = reader3.GetDecimal("Price")
column price is decimal in database
The GetDecimal method accepts a column ordinal only, not a column name. The correct option would be to get the ordinal for that name:
Dim testP As Decimal = reader3.GetDecimal(reader3.GetOrdinal("Price"))
Alternatively, you could just get the Object reference from the Item property and cast it:
Dim testP As Decimal = CDec(reader3("Price"))
this works
Dim testP As Decimal = Decimal.Parse(reader3("Price").ToString())
convert int to decimal
Convert.ToDecimal(reader3("Price").ToString());
Try using the index of the column you have retrieved. Say your Select looked like this
Select Name, Department, Item, Price From MyTable;
Then Price would be index 3. (Zero based)
Dim testP As Decimal = reader3.GetDecimal(3)
I'm trying to find the right regex to grepl weather a string contains digits[0-9] and the special character "-" only.
ex,
str1="00-25" #TRUE
str2="0a-2" #FALSE
I have tried
grepl("[^[:digit:]|-]",str2)
#[1] TRUE
thoughts?
You want to check if the string has only digit and -.
To create the ensemble, you need to use "[]" so :
[0-9-]
Now you want to check that every character of the string is in the ensemble you have created, in other term you want to start(^) and finish($) by this ensemble :
^[0-9-]$
Finally in the variable there is 1 or more character, so I use the "+" :
grepl("^[0-9-]+$",str)
I want to remove all control character from the given string. i don't want to use Replace method because it take multiple iteration.
Help me.
Thanks in advance.
You may not like it, but REPLACE is the simplest way to do it. I've used this code to strip non-printable characters from a string. This will replace the control characters with a space:
DEFINE VARIABLE str AS CHARACTER NO-UNDO.
DEFINE VARIABLE iLoop AS INTEGER NO-UNDO.
DO iLoop = 1 TO 31:
str = REPLACE(str, CHR(iLoop), " ").
END.
Since there are multiple control characters that have to be removed, it seems that any solution will involve multiple iterations.
Depending on what you define as a control character and what character set you are using this might do the trick. Or at least point you in a helpful direction:
define variable i as integer no-undo.
define variable n as integer no-undo.
define variable c as character no-undo.
define variable s as character no-undo.
define variable x as character no-undo.
s = "something with control characters in it".
x = "".
n = length( s ).
do i = 1 to n:
c = substring( s, i, 1 ).
if asc( c ) >= 32 and asc( c ) < 127 then
x = x + c.
end.
I cant seem to get my cost value to show decimal places. The code below for value "100.00" only gives me "100" When I debug the value of Session("TotalProductCost") is 100D?
Session("TotalProductCost") = Convert.ToDecimal(Cost1) + Convert.ToDecimal(Cost2) + Convert.ToDecimal(Cost3) + Convert.ToDecimal(Cost4) + Convert.ToDecimal(Cost5)
Dim TotalProductAmt As Decimal
Decimal.TryParse(Session("TotalProductCost"), TotalProductAmt)
Dim Res As Decimal = TotalProductAmt
TotalAmount = TotalProductAmt.ToString()
TotalAmount = Res.ToString
If you want the result of ToString to always include two decimal places, you need to tell ToString that. You can pass a string parameter which tells ToString what kind of result you expect, like this:
TotalAmount = Res.ToString("F2")
The above tells ToString "I want the result to be fixed to two decimal places". If you hand it the value 1000, the string you get back will be "1000.00".
Alternatively, if you also want the digits to be grouped such that 1000 results in "1,000.00" (or some other grouping system, depending on which country you're in), you can pass in "N2" as your parameter to ToString:
TotalAmount = Res.ToString("N2")
For details of this and other string formats you can pass to ToString, see the MSDN documentation.
As for why Visual Studio is showing you "100D", the D signifies that you're looking at a decimal value (as opposed to an integer or whatever).
I have string value in that I need to convert to double in VB.Net. Conditions are like below
string = "12345.00232232"
if condition is 3 (2 digits after decimal and comma)
display = 12,345.00
if condition is 5 (5 digits after decimal and comma)
display = 12,345.00232
If Condition is 7 ( 5 digits after decimal and no comma)
display = 12345.00232
How can I do that in VB.Net?
It sounds like you want to take a numeric input, convert it to double but then reformat it as a string based upon a numeric value for a specific style. Something probably like...
Public Function FormatNumericString(ByVal input As String, ByVal style As Integer) As String
Dim result As String = String.Empty
Dim temp As Double = Double.Parse(input) 'will throw on invalid input
Select Case style
Case 3
result = temp.ToString("#,##0.00")
Case 5
result = temp.ToString("#,##0.00000")
Case 7
result = temp.ToString("0.00000")
End Select
Return result
End Function
The basic thing is you have to convert the string to a double and use whatever formatting style you want. I've chosen to use double.Parse so that an exception would be thrown on an invalid input. double.TryParse could also be used, but it returns a true/false value rather than throwing an exception on an invalid input. It depends upon the behavior you want to follow.