本文整理匯總了VB.NET中System.Int16.Parse方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Int16.Parse方法的具體用法?VB.NET Int16.Parse怎麽用?VB.NET Int16.Parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。
在下文中一共展示了Int16.Parse方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: CultureInfo
Dim value As String
Dim number As Short
Dim style As NumberStyles
Dim provider As CultureInfo
' Parse string using "." as the thousands separator
' and " " as the decimal separator.
value = "19 694,00"
style = NumberStyles.AllowDecimalPoint Or NumberStyles.AllowThousands
provider = New CultureInfo("fr-FR")
number = Int16.Parse(value, style, provider)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
' '19 694,00' converted to 19694.
Try
number = Int16.Parse(value, style, CultureInfo.InvariantCulture)
Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
' Unable to parse '19 694,00'.
' Parse string using "$" as the currency symbol for en_GB and
' en-US cultures.
value = "$6,032.00"
style = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
provider = New CultureInfo("en-GB")
Try
number = Int16.Parse(value, style, CultureInfo.InvariantCulture)
Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
' Unable to parse '$6,032.00'.
provider = New CultureInfo("en-US")
number = Int16.Parse(value, style, provider)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
' '$6,032.00' converted to 6032.
示例2: ParseSample
' 導入命名空間
Imports System.Globalization
Module ParseSample
Public Sub Main()
Dim value As String
Dim style As NumberStyles
' Parse a number with a thousands separator (throws an exception).
value = "14,644"
style = NumberStyles.None
ParseToInt16(value, style)
style = NumberStyles.AllowThousands
ParseToInt16(value, style)
' Parse a number with a thousands separator and decimal point.
value = "14,644.00"
style = NumberStyles.AllowThousands Or NumberStyles.Integer Or _
NumberStyles.AllowDecimalPoint
ParseToInt16(value, style)
' Parse a number with a fractional component (throws an exception).
value = "14,644.001"
ParseToInt16(value, style)
' Parse a number in exponential notation.
value = "145E02"
style = style Or NumberStyles.AllowExponent
ParseToInt16(value, style)
' Parse a number in exponential notation with a positive sign.
value = "145E+02"
ParseToInt16(value, style)
' Parse a number in exponential notation with a negative sign
' (throws an exception).
value = "145E-02"
ParseToInt16(value, style)
End Sub
Private Sub ParseToInt16(value As String, style As NumberStyles)
Try
Dim number As Short = Int16.Parse(value, style)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}' with style {1}.", value, _
style.ToString())
Catch e As OverflowException
Console.WriteLine("'{0}' is out of range of the Int16 type.", value)
End Try
End Sub
End Module
輸出:
Unable to parse '14,644' with style None. Converted '14,644' to 14644. Converted '14,644.00' to 14644. 14,644.001' is out of range of the Int16 type. Converted '145E02' to 14500. Converted '145E+02' to 14500. 145E-02' is out of range of the Int16 type.
示例3:
Dim value As String
Dim number As Short
value = " 12603 "
Try
number = Short.Parse(value)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to convert '{0}' to a 16-bit signed integer.", _
value)
End Try
value = " 16,054"
Try
number = Short.Parse(value)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to convert '{0}' to a 16-bit signed integer.", _
value)
End Try
value = " -17264"
Try
number = Short.Parse(value)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to convert '{0}' to a 16-bit signed integer.", _
value)
End Try
輸出:
Converted ' 12603 ' to 12603. Unable to convert ' 16,054' to a 16-bit signed integer. Converted ' -17264' to -17264.
示例4:
Dim stringToConvert As String
Dim number As Short
stringToConvert = " 214 "
Try
number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
Console.WriteLine("'{0'} is out of range of the Int16 data type.", _
stringToConvert)
End Try
stringToConvert = " + 214"
Try
number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
Console.WriteLine("'{0'} is out of range of the Int16 data type.", _
stringToConvert)
End Try
stringToConvert = " +214 "
Try
number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
Console.WriteLine("'{0'} is out of range of the Int16 data type.", _
stringToConvert)
End Try
輸出:
Converted ' 214 ' to 214. Unable to parse ' + 214'. Converted ' +214 ' to 214.