Dim s As String = "3.0e+2"
Dim i1 As Int32 = Val(s)
Dim i2 As Int32 = s.Val
Dim i3 As Int32 = s.ToInteger
Break
shows that indeed i1 = 300, i2 = 300, and i3 is 3 not 300 like I would have expected. I changed my use of ToInteger to Val and things work as expected.
Actually, I am more surprised i1 (the Val(s) method) comes up with 300. I don’t need scientific notation often, but my general view of Val() on a string it is will quit at the first character that is not a digit, +/- sign, or decimal separator (period in the USA).
So I am more surprised that i1 is 300; I would have guessed it said 3.
Not that is will impact any of my programs either way…
It is not that difficult to understand that Scientific Notation IS a numeric value. Please don’t apply primary school maths concepts of numbers to modern programming, that’s ridiculous. Numbers are more than 0 to 9.
And in case you didn’t even read the documentation let me spell it out for you:
toInteger is stated to work the SAME as Val (I even had bolded it in the original post).
In general though, I shy away from Val() too because it also has never correctly handled separators. Have you ever tried Val(1,000.00) ?
The string 1,000.00 is a primary school math concept, and it fails too.
I’ll agree the docs say .ToInteger should be the same as Val() and it evidently is not. Will that lead to wrong results? Not in my code. But yes, I will concur it is a bug.
That is actually mentioned in the Val documentation:
For localized number formatting, use the CDbl function instead. Generally, you will use Val for converting internal data and use CDbl for converting data for input and output of user data.
The ToInt64 function stops reading the string at the first character it doesn’t recognize as part of a number. All other characters are automatically stripped.
it looks like ‘e’ is not recognized as part of a number.
There is Integer.FromString but that doesn’t work, not even the “1,234” sample code on that page: 63596 - Integer.FromString macOS API 2 locale bug causing exception on formatted strings
Edit: Windows works, mac doesn’t work (mentioned in the report)
I realize that, and is one reason I tend to NOT use Val() for anything unless I am really sure the data will be converted as expected. Since I have not needed scientific notation in over 40 years of my programming, it is not on my radar.
YMMV.
I’m not saying it should not be fixed; .ToInteger clearly does not act the same as Val() as the docs state. I just think Val() is also not as useful as some people assume it is. And if .ToInteger is fixed to operate the same as Val(), I’d avoid it too for most purposes.