在沙特阿拉伯 & 美國之間轉換 CultureInfo 貨幣



我有这样的东西:

price_2.Text = CDbl(price_2.Text(.ToString("C", current_culture(

问题/工作流程:

假设起始价格以美元为单位,假设price_2.Text是5.99美元。按下按钮时,current_culture将变为ar-SA。所以它会将 ($5.99( 转换为 (5.99 ريال(,现在是 (5.99 ريال( 并且我将current_culture更改为 en-US,这将崩溃,因为 CDbl 无法将 5.99 ريال 之后的符号转换为双精度。

任何想法我们如何从文化信息转换为货币?

您可以使用Decimal.TryParse()重载,该重载允许您指定从中派生货币格式的 NumberStyle 和 CultureInfo。

这允许解析方法了解如何解释提供的输入。

Dim numberStyle As NumberStyles = NumberStyles.Currency Or
NumberStyles.AllowCurrencySymbol Or
NumberStyles.AllowDecimalPoint Or
NumberStyles.AllowThousands
Dim CurrencyValue As Decimal
Dim arCInfo As New CultureInfo("ar-SA")
Dim usCInfo As New CultureInfo("en-US")
Dim InputType As String = If(price_2.Text.Contains("$"), "US", "AR")
Select Case InputType
Case "US"
If Decimal.TryParse(price_2.Text, numberStyle, usCInfo, CurrencyValue) Then
price_2.Text = CurrencyValue.ToString("C", arCInfo)
End If
Case "AR"
If Decimal.TryParse(price_2.Text, numberStyle, arCInfo, CurrencyValue) Then
price_2.Text = CurrencyValue.ToString("C", usCInfo)
End If
End Select

对全球化不是很熟悉,但这对我有用。只需在格式化之前获取数字,您就可以将区域性应用于数字本身。

Private dbl As Double
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
dbl =CDbl(price_2.Text)
Dim current_culture As New CultureInfo("ar-SA")
price_2.Text = dbl.ToString("C", current_culture)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim current_culture As New CultureInfo("en-US")
price_2.Text = dbl.ToString("C", current_culture)
End Sub

相关内容

  • 没有找到相关文章

最新更新