How and when are variant type are converted to regular data types - variant

When the actual data type of a variable will be decided?
For ex:
x=10 here x will hold integer
x="Hello" here x will hold string
My basic question is msgbox "2"+"3" is 23 because these are strings and + is for concatenation so the result is 23
Then how the result of msgbox "2"*"3" becomes 6? where the string will be converted to integers and returns 6

If you are talking about using Visual Basic (you have not specified a language) then here is what I believe is happening:
The MsgBox function is expecting a and Object to turn into a String. (or at least it is trying to convert a String before it is displayed). Since "+" is a legit operator for concatenation, the first example can be directly converted to a String and returned.
In the second example, the asterisk is not a legit String operator, so it then has to attempt to convert your String segments into Integers. It does, then multiplies them, then the MsgBox converts the numerical expression back into a String and displays it.

Related

My VBA is treating the letter D as a number, what is going on? [duplicate]

I had a strange error in a VB6 app this morning and it all stems from the fact that IsNumeric is not working as I expected. Can someone shed some light on why? To me this seems like a bug.
This code displays 4.15877E+62 in a message box:
Dim strMessage As String
strMessage = "0415877D57"
If IsNumeric(strMessage) Then
MsgBox CDbl(strMessage)
Else
MsgBox "not numeric"
End If
I am guessing that the runtime engine is incorrectly thinking that the D is in fact an E?
I think this is a bug though as the exact same code in VB.NET outputs not numeric
Is this a known issue with IsNumeric?
If you check the VB6 docs:
Note Floating-point values can be expressed as mmmEeee or mmmDeee, in which mmm is the mantissa and eee is the exponent (a power of 10). The highest positive value of a Single data type is 3.402823E+38, or 3.4 times 10 to the 38th power; the highest positive value of a Double data type is 1.79769313486232D+308, or about 1.8 times 10 to the 308th power. Using D to separate the mantissa and exponent in a numeric literal causes the value to be treated as a Double data type. Likewise, using E in the same fashion treats the value as a Single data type.
I've been using my own IsNumber function for a long time exactly because of this situation. IsNumeric can also return true for certain money symbols, like this: IsNumeric("$34.20").
My IsNumber function looks like this:
Public Function IsNumber(ByVal Data As String) As Boolean
If Data = "" Then
IsNumber = False
Exit Function
End If
IsNumber = IsNumeric(Data & "e0")
End Function
The idea here is... if there is already an e or d in the data, adding another will cause the data to NOT be numeric using the IsNumeric check. You can easily change this function to only allow for integers by replacing "e0" with ".0e0". Want just positive integers? then use this: IsNumeric("-" & Data & ".0e0")
The only downside of this method is that an empty string normally is not numeric, but when you append "e0" to it, it becomes numeric so you need to add a check for that, like I did in my code.
I suggest making a custom validator. Do you want to allow 0-9 only? What about negatives? Commas? I never cared for Microsoft's implementation, but I understand it.

Padding a hexadecimal string with zeros to a 6 character length

I have this:
function dec2hex(IN)
local OUT
OUT = string.format("%x",IN)
return OUT
end
and need IN to have padded zeros to string length of 6.
I can't use String.Utils or PadLeft. It's within an app called Watchmaker which uses a cut down version of Lua.
String formats in Lua work mostly just like in C. So to pad a number with zeros, just use %0n where n is the number of places. For example
print(string.format("%06x", 16^4-1))
will print 00ffff.
See chapter 20 The String Library of “Programming in Lua”, the reference of string.format, and the C reference for the printf family of functions for details.
If you store your format string locally you can call the format method on to the format string and the example of #Henri results in ("%06x"):format(0xffff)
print(("%06x"):format(0xffff)) -- Prints `00ffff`
You can write numbers in hex format. It is the same as C.

Performing arithmetic on string values

I would like to ask something about data types in Lua.
I get from serial link some message (command:value) like this:
tmp_string = "BRAKE:1"
then I parse this string to command and value in two different functions (one is for command and other one is for value). This is function for parsing value
function parser(value)
index = string.find(value, ":")
result = value.sub(value, index+1)
return result
end
I would like to now what sort of data type result is? If I use string match it works.
...if string.match(state, "1") then...
However it also works when I do something like this
x = (state*65536)/3.2808)
I thought the result is string, but I don't understand why it works also with numerical operations. Thank you in advance.
Lua 5.3 Reference Manual, §3.4.1 - Arithmetic Operators
With the exception of exponentiation and float division, the arithmetic operators work as follows: If both operands are integers, the operation is performed over integers and the result is an integer. Otherwise, if both operands are numbers or strings that can be converted to numbers (see §3.4.3), then they are converted to floats, the operation is performed following the usual rules for floating-point arithmetic (usually the IEEE 754 standard), and the result is a float.
Emphasis is mine.
When dealing with operations, Lua will attempt to convert string operands to floats, and if it works - it works. If it fails, you get an error.
>| '55' / 2
<| 27.5
>| 'foo' / 2
<| error: [string "return 'foo' / 2"]:1: attempt to perform arithmetic on a string value
If you want to be explicit about this (and safe) use tonumber, and handle the nil-case.
If you need to know the type of a value in Lua, you can pass the variable to type and check the resulting string.

MAXIMA string to integer conversion

I am reading a list with 2 integers from a file using readline(s) which returns a string. How does one convert the string of 2 integers back to 2 integers? Thanks.
I assume you have a string like "123 456". If so you can do this: map(parse_string, split(s)) where s is your string.
parse_string parses one Maxima expression, e.g. one integer. To get both integers, split the string, which makes a list, and then parse each element of the list.

How do I only replace the first instance of a number in a string?

I am taking a description of the weather using rainfall in millimeters and converting it to inches. I have worked out to calculate the rainfall in inches and replace it in the string. Because the description also has other numbers, I want to only replace the first instance of the number I converted (rainfall is listed first).
weatherConverted = weatherConverted.stringByReplacingOccurrencesOfString("\(precipitationInMillimeters)", withString: "\(precipitationInInches)")
precipitationInMillimeters is an Integer, hence the escape sequence. I know that the function rangeOfString(_:) will return the range of the first instance of the string I put in it, but I don't know the proper syntax for calling that function and getting it to return to the range parameter.

Resources