Search code examples
c#.netregexcultureinfodecimal-point

Replace dot(.) with comma(,) using RegEx?


I am working on a C# application. I want to change number decimal figure with comma(,) where i have dot(.) using regular expression.

For example:

Price= 100,00.56

As this international rule of representing numeric values but I Sweden they have different ways for numbers Like

Price= 100.00,56

So i want to change dot(.) into comma(,) and comma(,) into dot(.) using RegEx. Could guide me about this.


Solution

  • When formatting numbers, you should use the string format overload that takes a CultureInfo object. The culture name for swedish is "sv-SE", as can be seen here.

    decimal value = -16325.62m;
    Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("sv-SE")));
    

    Edit:

    As @OregonGhost points out - parsing out numbers should also be done with CultureInfo.