本文整理匯總了VB.NET中System.Double.Parse方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Double.Parse方法的具體用法?VB.NET Double.Parse怎麽用?VB.NET Double.Parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Double
的用法示例。
在下文中一共展示了Double.Parse方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: CultureInfo
Protected Sub OkToDouble_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToDouble.Click
Dim locale As String
Dim culture As CultureInfo
Dim number As Double
' Return if string is empty
If String.IsNullOrEmpty(Me.inputNumber.Text) Then Exit Sub
' Get locale of web request to determine possible format of number
If Request.UserLanguages.Length = 0 Then Exit Sub
locale = Request.UserLanguages(0)
If String.IsNullOrEmpty(locale) Then Exit Sub
' Instantiate CultureInfo object for the user's locale
culture = New CultureInfo(locale)
' Convert user input from a string to a number
Try
number = Double.Parse(Me.inputNumber.Text, culture.NumberFormat)
Catch ex As FormatException
Exit Sub
Catch ex As Exception
Exit Sub
End Try
' Output number to label on web form
Me.outputNumber.Text = "Number is " & number.ToString()
End Sub
示例2: Temperature
' 導入命名空間
Imports System.Globalization
Public Class Temperature
' Parses the temperature from a string. Temperature scale is
' indicated by 'F (for Fahrenheit) or 'C (for Celcius) at the end
' of the string.
Public Shared Function Parse(s As String, styles As NumberStyles, _
provider As IFormatProvider) As Temperature
Dim temp As New Temperature()
If s.TrimEnd(Nothing).EndsWith("'F") Then
temp.Value = Double.Parse(s.Remove(s.LastIndexOf("'"c), 2), _
styles, provider)
Else
If s.TrimEnd(Nothing).EndsWith("'C") Then
temp.Celsius = Double.Parse(s.Remove(s.LastIndexOf("'"c), 2), _
styles, provider)
Else
temp.Value = Double.Parse(s, styles, provider)
End If
End If
Return temp
End Function
' Declare private constructor so Temperature so only Parse method can
' create a new instance
Private Sub New
End Sub
Protected m_value As Double
Public Property Value() As Double
Get
Return m_value
End Get
Private Set
m_value = Value
End Set
End Property
Public Property Celsius() As Double
Get
Return (m_value - 32) / 1.8
End Get
Private Set
m_value = Value * 1.8 + 32
End Set
End Property
Public ReadOnly Property Fahrenheit() As Double
Get
Return m_Value
End Get
End Property
End Class
Public Module TestTemperature
Public Sub Main
Dim value As String
Dim styles As NumberStyles
Dim provider As IFormatProvider
Dim temp As Temperature
value = "25,3'C"
styles = NumberStyles.Float
provider = CultureInfo.CreateSpecificCulture("fr-FR")
temp = Temperature.Parse(value, styles, provider)
Console.WriteLine("{0} degrees Fahrenheit equals {1} degrees Celsius.", _
temp.Fahrenheit, temp.Celsius)
value = " (40) 'C"
styles = NumberStyles.AllowLeadingWhite Or NumberStyles.AllowTrailingWhite _
Or NumberStyles.AllowParentheses
provider = NumberFormatInfo.InvariantInfo
temp = Temperature.Parse(value, styles, provider)
Console.WriteLine("{0} degrees Fahrenheit equals {1} degrees Celsius.", _
temp.Fahrenheit, temp.Celsius)
value = "5,778E03'C" ' Approximate surface temperature of the Sun
styles = NumberStyles.AllowDecimalPoint Or NumberStyles.AllowThousands Or _
NumberStyles.AllowExponent
provider = CultureInfo.CreateSpecificCulture("en-GB")
temp = Temperature.Parse(value, styles, provider)
Console.WriteLine("{0} degrees Fahrenheit equals {1} degrees Celsius.", _
temp.Fahrenheit.ToString("N"), temp.Celsius.ToString("N"))
End Sub
End Module
示例3:
Dim value As String
value = Double.MinValue.ToString()
Try
Console.WriteLine(Double.Parse(value))
Catch e As OverflowException
Console.WriteLine($"{value} is outside the range of the Double type.")
End Try
value = Double.MaxValue.ToString()
Try
Console.WriteLine(Double.Parse(value))
Catch e As OverflowException
Console.WriteLine($"{value} is outside the range of the Double type.")
End Try
' Format without the default precision.
value = Double.MinValue.ToString("G17")
Try
Console.WriteLine(Double.Parse(value))
Catch e As OverflowException
Console.WriteLine($"{value} is outside the range of the Double type.")
End Try
輸出:
-1.79769313486232E+308 is outside the range of the Double type. 1.79769313486232E+308 is outside the range of the Double type. -1.79769313486232E+308
示例4: Temperature
Public Class Temperature
' Parses the temperature from a string in form
' [ws][sign]digits['F|'C][ws]
Public Shared Function Parse(ByVal s As String) As Temperature
Dim temp As New Temperature()
If s.TrimEnd(Nothing).EndsWith("'F") Then
temp.Value = Double.Parse(s.Remove(s.LastIndexOf("'"c), 2))
Else
If s.TrimEnd(Nothing).EndsWith("'C") Then
temp.Celsius = Double.Parse(s.Remove(s.LastIndexOf("'"c), 2))
Else
temp.Value = Double.Parse(s)
End If
End If
Return temp
End Function 'Parse
' The value holder
Protected m_value As Double
Public Property Value() As Double
Get
Return m_value
End Get
Set(ByVal Value As Double)
m_value = Value
End Set
End Property
Public Property Celsius() As Double
Get
Return (m_value - 32) / 1.8
End Get
Set(ByVal Value As Double)
m_value = Value * 1.8 + 32
End Set
End Property
End Class
示例5: Main
Public Sub Main()
' Set current thread culture to en-US.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
Dim value As String
Dim styles As NumberStyles
' Parse a string in exponential notation with only the AllowExponent flag.
value = "-1.063E-02"
styles = NumberStyles.AllowExponent
ShowNumericValue(value, styles)
' Parse a string in exponential notation
' with the AllowExponent and Number flags.
styles = NumberStyles.AllowExponent Or NumberStyles.Number
ShowNumericValue(value, styles)
' Parse a currency value with leading and trailing white space, and
' white space after the U.S. currency symbol.
value = " $ 6,164.3299 "
styles = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
ShowNumericValue(value, styles)
' Parse negative value with thousands separator and decimal.
value = "(4,320.64)"
styles = NumberStyles.AllowParentheses Or NumberStyles.AllowTrailingSign _
Or NumberStyles.Float
ShowNumericValue(value, styles)
styles = NumberStyles.AllowParentheses Or NumberStyles.AllowTrailingSign _
Or NumberStyles.Float Or NumberStyles.AllowThousands
ShowNumericValue(value, styles)
End Sub
Private Sub ShowNumericValue(value As String, styles As NumberStyles)
Dim number As Double
Try
number = Double.Parse(value, styles)
Console.WriteLine("Converted '{0}' using {1} to {2}.", _
value, styles.ToString(), number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}' with styles {1}.", _
value, styles.ToString())
End Try
Console.WriteLine()
End Sub
輸出:
Unable to parse '-1.063E-02' with styles AllowExponent. Converted '-1.063E-02' using AllowTrailingSign, AllowThousands, Float to -0.01063. Converted ' $ 6,164.3299 ' using Number, AllowCurrencySymbol to 6164.3299. Unable to parse '(4,320.64)' with styles AllowTrailingSign, AllowParentheses, Float. Converted '(4,320.64)' using AllowTrailingSign, AllowParentheses, AllowThousands, Float to -4320.64.
示例6: Tester
Public Class Tester
Public Shared Sub Main
Dim doubleParse As Double = Double.Parse("3.1416")
Console.WriteLine(doubleParse)
End Sub
End Class