I have almost a two part question. Firstly, I am trying to format a string that I have converted to an integer. I have the following code:
If Idx2 = 0 Then
response.Write(sName & vbKeyTab & " E01 " & vbKeyTab & CInt(oSplit(1)) & "</br>")
End If
This correctly displays my value in oSplit(1) as 75. I'd like to to display as 00075.00
I've tried this, but I get a 500 error:
Format(CInt(oSplit(1)), "00000.00")
My second question is regarding the CInt portion of my code. The data in my oSplit array is a string, and I am casting it to an integer. However, it seems as if CInt is rounding my values. Is there a parameter I can pass to CInt to prevent this?
Thank you.
To handle .05 then you want to convert it to a decimal not an Int:
Format(CDec(oSplit(1)), "00000.00")
Related
This question already has answers here:
VBScript implicit conversion in IF statement different from variable to literals?
(2 answers)
Closed 5 years ago.
I've run into a weird issue (IMHO) which may just be highlighting my ignorance when it comes to VBS vs. other programming languages. Here's what I have at the top of my .VBS file.
Option Explicit
Const MAXROWSFOREXCEL2010 = 1048576
I then have the following conditional check in a loop:
If numLines >= MAXROWSFOREXCEL2010 Then
wscript.echo "Inside the numLines If Then Statement"
wscript.echo "numLines = " & numLines & " >= " & MAXROWSFOREXCEL2010
As an example, let's say numLines is equal to 107563, which is clearly less than the 1 million plus value assigned to MAXROWSFOREXCEL2010 as a global const above.
For whatever reason, the IF statement is being executed, even when numLines is clearly less than the const.
However, if I remove the use of the const in the comparison, and just put the hard coded value of MAXROWSFOREXCEL2010 into the loop such as:
If numLines >= 1048576 Then
wscript.echo "Inside the numLines If Then Statement"
wscript.echo "numLines = " & numLines & " >= " & MAXROWSFOREXCEL2010
Then the IF statement is NOT entered incorrectly.
Can someone clue me in to why this is the case? Should I be somehow declaring the const as a particular data type? Is there some sort of truncation of the const going on?
I would be inclined to suggest the problem is numLines not being numeric rather than there being a problem with the Const value, in which case this question answers the problem.
From A:VBScript implicit conversion in IF statement different from variable to literals? by #cheran-shunmugavel
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.
With that in mind you simply need to make sure the variable numLines is explicitly a numeric value using an explicit cast.
'Explicitly cast variable to Long
numLines = CLng(numLines)
OK, I believe what's going on is a quirk of the way VBS attempts to determine data types. I also believe you're numLines variable is being evaluated as a String.
You can do Wscript.Echo TypeName(numLines) to verify this. I can replicate your issue when I set numLines equal to "107563" instead of 107563.
If a value is "boxed" in VBS, in the case of your const MAXROWSFOREXCEL2010, the engine will attempt to perform the comparison operation differently than if it's not. In this case, because numLines is a string and the comparison is against a boxed variable, it attempts to do a (bitwise) string comparison. If numLines is a string and the comparison is against an explicit Integer the comparison is done as a (bitwise) integer comparison.
If numLines was an Integer this problem doesn't happen.
Here are some more examples:
strOne = "1"
intOne = 1
If "1" = 1 Then WScript.Echo "true" Else WScript.Echo "false"
If "1" = intOne Then WScript.Echo "true" Else wscript.Echo "false"
If strOne = 1 Then WScript.Echo "true" Else wscript.Echo "false"
If strOne = intOne Then WScript.Echo "true" Else wscript.Echo "false"
Output
true
true
true
false
What is the Value of the below in vbscript
1)x=1+"1"
2)x="1"+"1"
3)x=1+"mulla"
Note:In the all above three cases I am using first variable as either string or integer and second on as always as string.
Case 1:Acting as a numeric and auto conversion to numeric during operation
enter code here
y=inputbox("Enter a numeric value","") Rem I am using 1 as input
x=1
msgbox x+y Rem value is 2
msgbox x*y Rem value is 1
Case 2:Acting as a String and no conversion to numeric during operation it fails
enter code here
y=inputbox("Enter a numeric value","") Rem I am using 1 as input
x=1
if y= x then
msgbox "pass"
else
msgbox "fail"
end if
Case 3:Acting as a String and explicit conversion to numeric during operation it passes
enter code here
y=inputbox("Enter a numeric value","") Rem I am using 1 as input
x=1
if Cint(y) = x then
msgbox "pass"
else
msgbox "fail"
end if
I need a logic reason for the different behaviors. but in other language it is straight forward and will work as expected
Reference: Addition Operator (+) (VBScript)
Although you can also use the + operator to concatenate two character strings, you should use the & operator for concatenation to eliminate ambiguity. When you use the + operator, you may not be able to determine whether addition or string concatenation will occur.
The type of the expressions determines the behavior of the + operator in the following way:
If Both expressions are numeric then the result is the addition of both numbers.
If Both expressions are strings then the result is the concatenation of both strings.
If One expression is numeric and the other is a string then an Error: type mismatch will be thrown.
When working with mixed data types it is best to cast your variables into a common data type using a Type Conversion Function.
I agree with most of what #thomas-inzina has said but the OP has asked for a more detailed explanation so here goes.
As #thomas-inzina point's out using + is dangerous when working with strings and can lead to ambiguity depending on how you combine different values.
VBScript is a scripting language and unlike it's big brothers (VB, VBA and VB.Net) it's typeless only (some debate about VB and VBA also being able to be typeless but that's another topic entirely) which means it uses one data type known as Variant. Variant can infer other data types such as Integer, String, DateTime etc which is where the ambiguity can arise.
This means that you can get some unexpected behaviour when using + instead of & as + is not only a concatenation operator when being used with strings but also a addition operator when working with numeric data types.
Dim x: x = 1
Dim y: y = "1"
WScript.Echo x + y
Output:
2
Dim x: x = "1"
Dim y: y = "1"
WScript.Echo x + y
Output:
11
Dim x: x = 1
Dim y: y = 1
WScript.Echo x + y
Output:
2
Dim x: x = 1
Dim y: y = "a"
WScript.Echo x + y
Output:
Microsoft VBScript runtime error (4, 5) : Type mismatch: '[string: "a"]'
Below is some vbscript that populates a field called Lot. At the moment when I run this the Lot field is displaying the ManualLot field as 123456.000000.
Does anyone know how I can change the code below to make 123456.000000 just 0000123456? So it removes the .000000 and starts with 0000 instead.
Function ManualLot_OnAfterChange()
If Backflushing.CodeObject.Quantity < 0 Then
Backflushing.CodeObject.Lot = Backflushing.CodeObject.ManualLot
Else
If Backflushing.CodeObject.Quantity > 0 Then
Backflushing.CodeObject.Lot = 0
End If
End If
End Function
You could use Split() get the value to the left of the decimal, and then use Left() to stick some zeros in front of it.
'how long the number should be
testLength=10
'The number we are changing
test="12345.00000"
'split(test, ".")(0) will get us the values to the left of the decimal
test=LEFT("000000000000", testLength-len(split(test, ".")(0))) & split(test, ".")(0)
msgbox(test)
Backflushing.CodeObject.Lot = RTrim (Backflushing.CodeObject.ManualLot)
Fixed it for me. Thanks for the help though JNevil got me on right track.
Dim strnumber
strnumber = "0.3"
Dim add
add = 0.1
Dim result
result = strnumber + add
MsgBox result
I want to get 0.4 as result, but get 3.1.
I tried clng(strnumber) and int(strnumber), nothing works. There is a simple solution for sure but I won't find it.
EDIT: Solution
result = CDbl(Replace(strnumber,".",",") + add
Has to do with your locale settings. Automatic conversion (as well as explicit one) observes it in the same manner as in CStr() function.
E.g. in my locale CStr( 0.3) results to 0,3 that is invert to CDbl("0,3") while CDbl("0.3") results to an error.
BTW: always use option explicit and, for debugging purposes, On Error Goto 0
Following below procedures can help:
Replacing the dot(".") with comma (",") in the string
change the string to double by Cdbl
example:
dim a,b,c
a="10.12"
b="5.05"
a=Replace(a,".",",")
b= Replace(b,".",",")
c=Cdbl(a)+Cdbl(b)
msgbox c
You want to add two numbers. So you should use numbers (and not a string (strnumber) and a number (add):
>> n1 = 0.3
>> n2 = 0.1
>> r = n1 + n2
>> WScript.Echo r
>>
0,4
As you can see from the output (0,4), I'm using a locale (German) that uses "," as decimal 'point'. But literals always use ".". So by using the proper data types you can write your scripts in a language/locale independent fashion as long as you don't need to process external string data (from a file or user input). Then you have to modify the input before you feed it to a conversion function like CDbl(). For simple cases that can be done with Replace(inp, badmarker, goodmarker).
P.S. You said you " tried clng(strnumber) and int(strnumber)". You should have tried CDbl(). CLng() tries to get a long integer (cf. CInt()), Int() removes/rounds the fraction from number.
Original:
Using VB6
If rsCardEvent(4).Value = Str Then
TimOut = rsCardEvent(4)
Else
TimeOut = Left(TimOut, 2) & ":" & Mid(TimOut, 3, 2) & ":" & Right(TimOut, 2)
End If
Getting Type MisMatch Error.
How To Find Record Set Value is String or Number
Exactly i need
If Number means print number like Time Format (HH:MM:SS)
else
print string value
Coding Help for the above condition
Edited Version:
I'm working with an ADO.Recordset object and am trying to determine the data type of a column at run-time. I need to handle the column value differently in my code depending on its underlying data type. If the column value is a string, I want to work with the value as-is. If it is a number, I want to treat the number as an packed time and convert it to HH:MM:SS format (i.e. the number 120537 would be converted to the string "12:05:37").
Below is some example code that demonstrates what I want to achieve. However, when I run this code I get a "Type Mismatch" error:
If rsCardEvent(4).Value = Str Then
TimOut = rsCardEvent(4)
Else
TimeOut = Left(TimOut, 2) & ":" & Mid(TimOut, 3, 2) & ":" & Right(TimOut, 2)
End If
Have a look at the Visual Basic 6 function library. There are functions that can help you determine the underlying type of a value.
There are quite a few but you might find these useful:
IsNumeric
IsDate
Based on this article, if rsCardEvent is an ADO recordset, you could check the Type property. Something like this:
Select Case rsCardEvent(4).Type
Case adBSTR, adChar, adVarChar, adWChar, _
adVarWChar, adLongVarChar, adLongVarWChar
' It is a string '
Case Else
' It is not a string '
End Select
You can use the IsNumeric function available in VB6.
How about:
If TypeName(rsCardEvent(4).Value) = "String" then
http://msdn.microsoft.com/en-us/library/5422sfdf.aspx