0

I have something like:

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

Issue/Workflow:

And lets assume the beginning price is in dollars so lets say price_2.Text is $5.99. And when pressing a button the current_culture will change to ar-SA. So it will convert ($5.99) to (5.99 ريال‎), now that it is (5.99 ريال‎) and I change the current_culture to be en-US, this will crash, since the CDbl cannot convert that symbol after 5.99 ريال‎ to a double.

Any ideas how we can convert from CultureInfo's for currency?

Omid CompSCI
  • 1,861
  • 3
  • 17
  • 29
  • `Double.Parse()` (or TryParse) would seem to be more appropriate than that legacy `CDbl`. Also, if it *is* currency, `Decimal` would seem to be a better choice – Ňɏssa Pøngjǣrdenlarp May 05 '18 at 01:40
  • Changing culture invariably also requires re-creating the UI. So it is not a problem, you'll retrieve the value from your data model, not the view. – Hans Passant May 05 '18 at 01:48
  • @HansPassant not quite following that, so how do we retrieve the value from "data model", what does data model refer to. – Omid CompSCI May 05 '18 at 01:49
  • @Plutonix so this would work for strings being converted to a double, with this characters in it? Trying it now. – Omid CompSCI May 05 '18 at 01:51
  • Im thinking instead of doing CDbl(price_2.Text) since I am not doing any real time conversions, I could make the numbers hardcoded, and do price_2.Text = CDbl("5.99").ToString("C", current_culture) – Omid CompSCI May 05 '18 at 02:01

2 Answers2

1

You could use the Decimal.TryParse() overload that lets you to specify a NumberStyle and the CultureInfo from which to derive the currency format.

This allows the parsing method to understand how to interpret the input provided.

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
Jimi
  • 29,621
  • 8
  • 43
  • 61
1

Not very familiar with globalization but this worked for me. Just grab the number before formatting then you can apply the culture to the number itself.

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
Mary
  • 14,926
  • 3
  • 18
  • 27