本文整理汇总了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