Am 12.07.2010 12:27, schrieb mp:
thanks again!
I just discovered the cType function by discovering the suggested fixes for
syntax errors the ide provided when i converted a project from Option strict
off to option strict on, and fixing the many errors discovered.
I'll play with that soon to see if ctype(3.14, integer) is like int(3.14) or
fix(3.14) - truncate or round.
anyway thanks again for the info.
For the conversion of the basic data types, CInt, CDbl etc are less to
write than CType. CInt(double) is the same as CType(double, integer).
As we've already discussed, these are keywords. Depending on the source
and destination type, the required code is generated. In the case of
CInt(double), Math.Round is called, followed by the actual conversion
to Integer.
Concerning the "real" functions (those in the MSVB library): I wouldn't
use them all. For example, Int (full name Microsoft.VisualBasic.Conversion.Int)
only calls Math.Floor. Therefore I prefer the direct call to Math.Floor.
I don't find Fix in Math.*, so MSVB.Fix is still "valid".
As to the differences, try this:
Public Class Main
Shared Sub Main()
Const fmt As String = "{0,10} {1,15} {2,10} {3,10} {4,10} {5,10}"
Debug.Print(fmt, "value", "Math.Floor", "Int", "Fix", "Cint", "Round")
For i = -30 To 30
Dim d = i / 10
Dim o1 = Math.Floor(d)
Dim o2 = Int(d)
Dim o3 = Fix(d)
Dim o4 = CInt(d)
Dim o5 = Math.Round(d)
Debug.Print(fmt, d, o1, o2, o3, o4, o5)
Next
End Sub
End Class
As you can see, CInt and Math.Round return the same value, but
CInt also converts to an Integer whereas Math.Round returns a Double.