在沙特阿拉伯和沙特阿拉伯之间转换CultureInfo货币我们

时间:2018-05-05 01:24:54

标签: vb.net 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ريال之后将该符号转换为一双。

我们如何转换CultureInfo的货币?

2 个答案:

答案 0 :(得分:1)

您可以使用Decimal.TryParse()重载来指定导出货币格式的NumberStyleCultureInfo

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

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

答案 1 :(得分:1)

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

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