8

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.

Richard Ev
  • 52,939
  • 59
  • 191
  • 278
Mohsin JK
  • 561
  • 2
  • 8
  • 18

6 Answers6

25

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.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Virtual +1, sadly I'm out of votes today. But know you earned it. :) – sarnold Aug 04 '10 at 09:50
  • 3
    +1 for the canonical solution. It's important to note though that this is not only the solution for *formatting*, but also for *parsing* numbers. – OregonGhost Aug 04 '10 at 10:00
6

Not a RegEx solution but from my experience - more correct:

public static string CheckDecimalDigitsDelimiter(this string instance)
{
    var sv = new CultureInfo("sv-SE");
    var en = new CultureInfo("en-US");

    decimal d;
    return (!Decimal.TryParse(instance, NumberStyles.Currency, sv, out d) &&
            Decimal.TryParse(instance, NumberStyles.Currency, en, out d)) ?
        d.ToString(sv) : // didn't passed by SV but did by EN
        instance;
}

What does this method do? It ensures that if given string is incorrect Sweden string but is correct English - convert it to Sweden, e.g. 100,00 -> 100,00 but 100.00 -> 100,00.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
5

You can do this even without regex. For example

var temp = price.Replace(".", "<TEMP>");
var temp2 = temp.Replace(",", ".");
var replaced = temp2.Replace("<TEMP>", ",");
sukru
  • 2,229
  • 14
  • 15
5

Not sure what 100,00.56 represents, did you mean 10.000,56?
To answer your question:

For such a simple task, why use RegEx? You can do it much easier:

string oldValue = "100,00.56";
char dummyChar = '&'; //here put a char that you know won't appear in the strings
var newValue = oldValue.Replace('.', dummyChar)
                       .Replace(',', '.')
                       .Replace(dummyChar, ',');

Edit I agree with @Oded, for formatting numbers use the CultureInfo class.

cjk
  • 45,739
  • 9
  • 81
  • 112
scripni
  • 2,144
  • 2
  • 19
  • 25
  • `100,00.56` is an English representation, `10.000,56` - Sweden/Russian/etc – abatishchev Aug 04 '10 at 10:05
  • 1
    @abati: I think he was talking about the fact that the OP's examples have only two digits between the thousands separator and the decimal separator. It's probably just a typo. – Alan Moore Aug 04 '10 at 12:14
  • fixed dummyChar to be a variable not a literal in the last line. – cjk Aug 05 '10 at 06:59
5

Also have a look at

System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator
Grozz
  • 8,317
  • 4
  • 38
  • 53
3

Do not rely on RegExp for this kind of thing :) Use the build in cultures fx:

decimal s = decimal.Parse("10,000.56", NumberStyles.Currency, CultureInfo.GetCultureInfo("en-US"));
string output = s.ToString("N",CultureInfo.GetCultureInfo("da-DK"));

en-US will parse it correctly and da-DK uses the other kind of representation. I live in DK and therefore use that but you should use the culture which fits your output.

Lasse Espeholt
  • 17,622
  • 5
  • 63
  • 99