Question

I am trying here to parse a number using the Gabon currency formating.

The format uses "." for group separations and no decimals.

Here is an example :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Threading;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            CultureInfo ci = new CultureInfo("fr-FR");

            ci.NumberFormat.CurrencyGroupSeparator = ".";
            ci.NumberFormat.CurrencyDecimalDigits = 0;
            ci.NumberFormat.CurrencySymbol = "CFA";

            Thread.CurrentThread.CurrentCulture = ci;
            Thread.CurrentThread.CurrentUICulture = ci;

            double.Parse("300.000", ci).ToString("C"); 
                    // gives me a FormatException
        }
    }
}

Is there something I am missing ?

Was it helpful?

Solution

add this: ci.NumberFormat.NumberGroupSeparator = ".";

OTHER TIPS

In your case, you have to help .NET a little - when simply using Parse like that, it assumes you want to get a number. French culture uses , as a decimal separator and that is the reason your code throws an exception.

Try this, instead:

double.Parse("300.000", NumberStyles.Currency, ci).ToString("C");

Now, the string will get correctly parsed as currency, respecting the currency rules you have specified in the ci culture.

And - as others have said, you should really use decimal when dealing with currency. Double is simply not precise enough.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top