Newie
Newie

Reputation: 61

Format currency for different countries

I'm using Xamarin Forms and I tried to format a string (in XAML view) for currency in form: 1.235.436,00 where comma , is the decimal separator. At first, I used StringFormat{0:C2} and result was: $1,235,436.00 which is for US. After, I tried StringFormat{0:#,0.#0}, result: 1,235,436.00.

At last, I tried to use same logic by swapping the dot and comma {0:#.0,#0}, and I got as result: 1235436.000.

So how I can format for currency for France, Spain, Swedish, etc. which is 0.000.000,00 €/kr?

XAML code:

Label Text="{Binding value, StringFormat='{0:C2}.'}"

Which is not correct in my case.

Upvotes: 4

Views: 9624

Answers (4)

Magnus7
Magnus7

Reputation: 69

Add line the code in the xaml.cs This convert the culture

CultureInfo myCurrency = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentCulture = myCurrency;

In the xaml

<Label Text="{Binding value, StringFormat='{0:#,0.#0}'}"

Result: 5,245.00

I get this information through a list

Upvotes: 1

Huu Bao Nguyen
Huu Bao Nguyen

Reputation: 1191

you try to way:

// Get Present Culture in app.

var ci = new CultureInfo(App.DeviceCulture);

// Convert to format String.

stringValue = ConvertCultureCurrencyToString(stringValue, ci);

// Create method convert to format

public static string ConvertCultureCurrencyToString(string stringValue, CultureInfo ci)
    {
        try
        {
            if (string.IsNullOrEmpty(stringValue))
                return "0";

            stringValue = System.Convert.ToString(stringValue, ci);
            // currency -> double (format to double)
            var currency = decimal.Parse(stringValue, NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, ci);

            stringValue = currency.ToString("#,###.###", ci);

            if (stringValue.FirstOrDefault() == '.' || stringValue.FirstOrDefault() == ',')   
                stringValue = $"0{stringValue}";

            return stringValue;
        }
        catch
        {
            return "0";
        }

    }

Upvotes: 0

Racil Hilan
Racil Hilan

Reputation: 25351

The format {0:C2} that you used is the right one. To change the format based on the region, you need to change the culture on the thread. It looks like you have it set to the default en-US like you said. You can check it like this:

  CultureInfo current = CultureInfo.CurrentCulture;
  Console.WriteLine("The current culture is {0}", current.Name);

To change it to France:

  CultureInfo.CurrentCulture = new CultureInfo("fr-FR");

And now your value will be automatically formatted like 1.235.436,00.

Upvotes: 2

Martin Zikmund
Martin Zikmund

Reputation: 39092

You cannot do that directly in the binding using StringFormat. You can either implement a custom IValueConverter or create a string property and then implement the formatting in code:

1235436.ToString("C2", CultureInfo.CreateSpecificCulture("es-ES"));

This will format the number as a currency with specific Spanish culture display setting.

You can also modify the a default culture format to force a specific formatting:

var culture = CultureInfo.CreateSpecificCulture("en-US");
culture.NumberFormat.CurrencyDecimalSeparator = ",";
culture.NumberFormat.CurrencyGroupSeparator = ".";

But you should not need to do this. The culture defaults are set in the way in which the users expect the content to be displayed. Changes in the default formats can cause misunderstandings.

Upvotes: 7

Related Questions