共用方式為


類型轉換函式 (Visual Basic)

這些函數是內嵌編譯的,這意味著轉換代碼是評估表達式的代碼的一部分。 有時沒有呼叫程序來完成轉換,這會改善效能。 每個函數都會將運算式強制為特定資料類型。

語法

CBool(expression)
CByte(expression)
CChar(expression)
CDate(expression)
CDbl(expression)
CDec(expression)
CInt(expression)
CLng(expression)
CObj(expression)
CSByte(expression)
CShort(expression)
CSng(expression)
CStr(expression)
CUInt(expression)
CULng(expression)
CUShort(expression)

部分

expression
必須的。 來源資料類型的任何運算式。

傳回值資料類型

函數名稱會決定其傳回值的資料類型,如下表所示。

函數名稱 傳回資料類型 引數範圍expression
CBool 布林資料類型 任何有效的 CharString 數值運算式。
CByte 位元組資料類型 Byte.MinValue (0) 到 Byte.MaxValue (255) (無符號);小數部分四捨五入。1

從 Visual Basic 15.8 開始,Visual Basic 會使用函 CByte 式將浮點到位元組轉換的效能最佳化;如需詳細資訊,請參閱 備註 一節。 如需範例,請參閱 CInt 範例 一節。
CChar 字元資料類型 任何有效 CharString 運算式,只會轉換 a String 的第一個字元,值可以是 0 到 65535 (無符號)。
CDate 日期資料類型 日期和時間的任何有效表示。
CDbl 雙精度資料類型 負值為-1.79769313486231570E+308至-4.94065645841246544E-324;4.94065645841246544E-324 到 1.79769313486231570E+308 表示正值。
CDec 十進位資料類型 +/-79,228,162,514,264,337,593,543,950,335 表示零比例數字,即沒有小數位數。 對於小數點後 28 位的數字,範圍為 +/-7.9228162514264337593543950335。 最小可能的非零數是 0.00000000000000000000000000000000000000000000000000000001 (+/-1E-28)。
CInt 整數資料類型 Int32.MinValue (-2,147,483,648) 至 Int32.MaxValue (2,147,483,647);小數部分會四捨五入。1

從 Visual Basic 15.8 開始,Visual Basic 會使用函 CInt 式最佳化浮點到整數轉換的效能;如需詳細資訊,請參閱 備註 一節。 如需範例,請參閱 CInt 範例 一節。
CLng 長資料類型 Int64.MinValue (-9,223,372,036,854,775,808) 到 Int64.MaxValue (9,223,372,036,854,775,807);小數部分是四捨五入的。1

從 Visual Basic 15.8 開始,Visual Basic 會使用函數 CLng 將浮點到 64 位整數轉換的效能最佳化;如需詳細資訊,請參閱 備註 一節。 如需範例,請參閱 CInt 範例 一節。
CObj 物件資料類型 任何有效的運算式。
CSByte SByte 資料類型 SByte.MinValue (-128) 到 SByte.MaxValue (127);小數部分是四捨五入的。1

從 Visual Basic 15.8 開始,Visual Basic 會使用函 CSByte 式將浮點到帶正負號位元組轉換的效能最佳化;如需詳細資訊,請參閱 備註 一節。 如需範例,請參閱 CInt 範例 一節。
CShort 簡短資料類型 Int16.MinValue (-32,768) 到 Int16.MaxValue (32,767);小數部分是四捨五入的。1

從 Visual Basic 15.8 開始,Visual Basic 會使用函數 CShort 將浮點到 16 位整數轉換的效能最佳化;如需詳細資訊,請參閱 備註 一節。 如需範例,請參閱 CInt 範例 一節。
CSng 單一資料類型 負值為 -3.402823E+38 至 -1.401298E-45;1.401298E-45 到 3.402823E+38 表示正值。
CStr 字串資料類型 傳回 取決於CStrexpression引數。 請參閱 CStr 函式的傳回值
CUInt UInteger 資料類型 UInt32.MinValue (0) 到 UInt32.MaxValue (4,294,967,295) (無符號);小數部分四捨五入。1

從 Visual Basic 15.8 開始,Visual Basic 會使用函數 CUInt 將浮點到無正負號整數轉換的效能最佳化;如需詳細資訊,請參閱 備註 一節。 如需範例,請參閱 CInt 範例 一節。
CULng ULong 資料類型 UInt64.MinValue (0) 到 UInt64.MaxValue (18,446,744,073,709,551,615) (無符號);小數部分四捨五入。1

從 Visual Basic 15.8 開始,Visual Basic 會使用函數 CULng 將浮點到無正負號長整數轉換的效能最佳化;如需詳細資訊,請參閱 備註 一節。 如需範例,請參閱 CInt 範例 一節。
CUShort UShort 資料類型 UInt16.MinValue (0) 到 UInt16.MaxValue (65,535) (無符號);小數部分是四捨五入的。1

從 Visual Basic 15.8 開始,Visual Basic 會使用函數 CUShort 將浮點到不帶正負號的 16 位整數轉換效能最佳化;如需詳細資訊,請參閱 備註 一節。 如需範例,請參閱 CInt 範例 一節。

1 小數部分可以受到一種特殊類型的舍入,稱為 莊家舍入。 請參閱「備註」以取得更多資訊。

備註

一般而言,您應該優先使用 Visual Basic 類型轉換函式,而不是在類別或個別類型結構或類別上Convert使用 .NET Framework 方法ToString(),例如 。 Visual Basic 函數旨在實現與 Visual Basic 程式碼的最佳互動,而且它們也會讓您的原始程式碼更短且更易於閱讀。 此外,.NET Framework 轉換方法不一定會產生與 Visual Basic 函式相同的結果,例如,當轉換為 BooleanInteger時。 如需詳細資訊,請參閱 疑難排解資料類型

從 Visual Basic 15.8 開始,當您將下列方法傳回的 or 值傳遞Single至其中一個整數轉換函式 (CByteCShortCSByteCIntCLngCUShortCUIntCULng時,浮點到整數轉換的效能會最佳化:Double

此最佳化可讓執行大量整數轉換的程式碼執行速度高達兩倍。 下列範例說明這些最佳化的浮點到整數轉換:

Dim s As Single = 173.7619
Dim d As Double = s

Dim i1 As Integer = CInt(Fix(s))               ' Result: 173
Dim b1 As Byte = CByte(Int(d))                 ' Result: 173
Dim s1 AS Short = CShort(Math.Truncate(s))     ' Result: 173
Dim i2 As Integer = CInt(Math.Ceiling(d))      ' Result: 174
Dim i3 As Integer = CInt(Math.Round(s))        ' Result: 174

行為

  • 脅迫。 一般而言,您可以使用資料類型轉換函式,將作業的結果強制為特定資料類型,而不是預設資料類型。 例如,在通常會發生單精確度、雙精確度或整數算術的情況下,用於 CDec 強制執行十進制算術。

  • 轉換失敗。 如果傳遞給函數的 超出expression要轉換到的資料類型範圍,則會發生 。OverflowException

  • 分數部分。 當您將非整數值轉換為整數類型時,整數轉換函數 (CByteCIntCShortCSByteCUIntCLngCULngCUShort) 會移除小數部分,並將值四捨五入至最接近的整數。

    如果小數部分恰好是 0.5,則整數轉換函數將其四捨五入到最接近的偶數整數。 例如,0.5 四捨五入為 0,1.5 和 2.5 都四捨五入為 2。 這有時稱為 莊家舍入,其目的是補償將許多此類數字相加時可能累積的偏差。

    CInt 並且 CLng 與 and Int 函數不同,後者 Fix 截斷而不是四捨五入數字的小數部分。 此外, Fix 並且 Int 始終返回與您傳入的數據類型相同的值。

  • 日期/時間轉換。 使用函數 IsDate 來判斷值是否可以轉換為日期和時間。 CDate 辨識日期常值和時間常值,但無法辨識數值。 若要將 Visual Basic 6.0 DateDate 轉換為 Visual Basic 2005 或更新版本中的值,您可以使用該 DateTime.FromOADate 方法。

  • 中性日期/時間值。 日期資料類型一律同時包含日期和時間資訊。 為了進行類型轉換,Visual Basic 會將 1/1/0001 (第 1 年的 1 月 1 日) 視為日期的 中性值 ,而 00:00:00 (午夜) 是時間的中性值。 如果您將值轉換為 Date 字串, CStr 則不會在產生的字串中包含中性值。 例如,如果您轉換 #January 1, 0001 9:30:00# 成字串,則結果為「上午 9:30:00」;日期資訊會隱藏。 但是,日期資訊仍以原始 Date 值存在,並且可以透過函數等 DatePart 函數來恢復。

  • 文化敏感性。 涉及字串的類型轉換函式會根據應用程式目前的文化特性設定來執行轉換。 例如, CDate 根據系統的地區設定來辨識日期格式。 您必須以正確的順序提供地區設定的日、月和年,否則日期可能無法正確解譯。 如果長日期格式包含星期幾字串,例如「星期三」,則無法辨識。

    如果您需要以地區設定所指定格式以外的格式轉換值的字串表示法,則無法使用 Visual Basic 類型轉換函式。 若要這樣做,請使用 ToString(IFormatProvider) 該值類型的 and Parse(String, IFormatProvider) 方法。 例如,在將字串Double轉換為 時使用Double.Parse,以及在將類型Double值轉換為字串時使用Double.ToString

CType 運算子

CType 運算子會採用第二個引數 typename,並強制expression執行為 typename,其中typename可以是存在有效轉換的任何資料類型、結構、類別或介面。

如需與其他類型轉換關鍵字的 CType 比較,請參閱 DirectCast 運算子TryCast 運算子

CBool 範例

下列範例會使用函數 CBool 將運算式轉換成 Boolean 值。 如果運算式的評估值為非零值, CBool 則會傳回 True;否則會傳回 False

Dim a, b, c As Integer
Dim check As Boolean
a = 5
b = 5
' The following line of code sets check to True.
check = CBool(a = b)
c = 0
' The following line of code sets check to False.
check = CBool(c)

CByte 範例

下列範例會使用函數 CByte 將運算式 Byte轉換為 .

Dim aDouble As Double
Dim aByte As Byte
aDouble = 125.5678
' The following line of code sets aByte to 126.
aByte = CByte(aDouble)

CChar 範例

下列範例會使用函數 CChar 將運算式的 String 第一個字元轉換成 Char 類型。

Dim aString As String
Dim aChar As Char
' CChar converts only the first character of the string.
aString = "BCD"
' The following line of code sets aChar to "B".
aChar = CChar(aString)

輸入 CChar 引數必須是資料類型 CharString。 您無法用來 CChar 將數字轉換為字元,因為 CChar 無法接受數值資料類型。 以下範例取得一個代表碼點(字元碼)的數字,並將其轉換為對應的字元。 它使用函數 InputBox 來取得數字字串, CInt 將字串轉換為類型 Integer,並將 ChrW 數字轉換為類型 Char

Dim someDigits As String
Dim codePoint As Integer
Dim thisChar As Char
someDigits = InputBox("Enter code point of character:")
codePoint = CInt(someDigits)
' The following line of code sets thisChar to the Char value of codePoint.
thisChar = ChrW(codePoint)

CDate 範例

下列範例會使用函 CDate 式將字串轉換成 Date 值。 一般而言,不建議將日期和時間硬式編碼為字串 (如本範例所示)。 請改用日期常值和時間常值,例如 #Feb 12、1969# 和 #4:45:23 PM#。

Dim aDateString, aTimeString As String
Dim aDate, aTime As Date
aDateString = "February 12, 1969"
aTimeString = "4:35:47 PM"
' The following line of code sets aDate to a Date value.
aDate = CDate(aDateString)
' The following line of code sets aTime to Date value.
aTime = CDate(aTimeString)

CDbl 範例

Dim aDec As Decimal
Dim aDbl As Double
' The following line of code uses the literal type character D to make aDec a Decimal.
aDec = 234.456784D
' The following line of code sets aDbl to 1.9225456288E+1.
aDbl = CDbl(aDec * 8.2D * 0.01D)

CDec 範例

下列範例會使用函數 CDec 將數值 Decimal轉換為 。

Dim aDouble As Double
Dim aDecimal As Decimal
aDouble = 10000000.0587
' The following line of code sets aDecimal to 10000000.0587.
aDecimal = CDec(aDouble)

示例

下列範例會使用函數 CInt 將值 Integer轉換為 。

Dim aDbl As Double
Dim anInt As Integer
aDbl = 2345.5678
' The following line of code sets anInt to 2346.
anInt = CInt(aDbl)

CLng 範例

下列範例會使用函數 CLng 將值 Long轉換為 。

Dim aDbl1, aDbl2 As Double
Dim aLng1, aLng2 As Long
aDbl1 = 25427.45
aDbl2 = 25427.55
' The following line of code sets aLng1 to 25427.
aLng1 = CLng(aDbl1)
' The following line of code sets aLng2 to 25428.
aLng2 = CLng(aDbl2)

CObj 範例

下列範例會使用函數 CObj 將數值 Object轉換為 。 Object變數本身只包含一個四位元組指標,該指標指向Double指派給它的值。

Dim aDouble As Double
Dim anObject As Object
aDouble = 2.7182818284
' The following line of code sets anObject to a pointer to aDouble.
anObject = CObj(aDouble)

CSByte 範例

下列範例會使用函數 CSByte 將數值 SByte轉換為 。

Dim aDouble As Double
Dim anSByte As SByte
aDouble = 39.501
' The following line of code sets anSByte to 40.
anSByte = CSByte(aDouble)

CShort 範例

下列範例會使用函數 CShort 將數值 Short轉換為 。

Dim aByte As Byte
Dim aShort As Short
aByte = 100
' The following line of code sets aShort to 100.
aShort = CShort(aByte)

CSng 範例

下列範例會使用函數 CSng 將值 Single轉換為 。

Dim aDouble1, aDouble2 As Double
Dim aSingle1, aSingle2 As Single
aDouble1 = 75.3421105
aDouble2 = 75.3421567
' The following line of code sets aSingle1 to 75.34211.
aSingle1 = CSng(aDouble1)
' The following line of code sets aSingle2 to 75.34216.
aSingle2 = CSng(aDouble2)

CStr 範例

下列範例會使用函數 CStr 將數值 String轉換為 。

Dim aDouble As Double
Dim aString As String
aDouble = 437.324
' The following line of code sets aString to "437.324".
aString = CStr(aDouble)

下列範例會使用函數CStr將值String轉換為Date值。

Dim aDate As Date
Dim aString As String
' The following line of code generates a COMPILER ERROR because of invalid format.
' aDate = #February 12, 1969 00:00:00#
' Date literals must be in the format #m/d/yyyy# or they are invalid.
' The following line of code sets the time component of aDate to midnight.
aDate = #2/12/1969#
' The following conversion suppresses the neutral time value of 00:00:00.
' The following line of code sets aString to "2/12/1969".
aString = CStr(aDate)
' The following line of code sets the time component of aDate to one second past midnight.
aDate = #2/12/1969 12:00:01 AM#
' The time component becomes part of the converted value.
' The following line of code sets aString to "2/12/1969 12:00:01 AM".
aString = CStr(aDate)

CStr 一律會 Date 以目前地區設定的標準簡短格式轉譯值,例如「6/15/2003 4:35:47 PM」。 不過, CStr 會隱藏日期的 中性值 1/1/0001 和時間的 00:00:00。

如需 所傳回 CStr值的詳細資訊,請參閱 CStr 函式的傳回值

CUInt 範例

下列範例會使用函數 CUInt 將數值 UInteger轉換為 。

Dim aDouble As Double
Dim aUInteger As UInteger
aDouble = 39.501
' The following line of code sets aUInteger to 40.
aUInteger = CUInt(aDouble)

CULng 範例

下列範例會使用函數 CULng 將數值 ULong轉換為 。

Dim aDouble As Double
Dim aULong As ULong
aDouble = 39.501
' The following line of code sets aULong to 40.
aULong = CULng(aDouble)

CUShort 範例

下列範例會使用函數 CUShort 將數值 UShort轉換為 。

Dim aDouble As Double
Dim aUShort As UShort
aDouble = 39.501
' The following line of code sets aUShort to 40.
aUShort = CUShort(aDouble)

另請參閱