Replace dot(.) with comma(,) using RegEx?
Asked Answered
M

6

9

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.

Macaronic answered 4/8, 2010 at 9:44 Comment(0)
P
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.

Pumpkinseed answered 4/8, 2010 at 9:48 Comment(2)
Virtual +1, sadly I'm out of votes today. But know you earned it. :)Catching
+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.Earleanearleen
P
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.

Preservative answered 4/8, 2010 at 10:0 Comment(0)
S
5

You can do this even without regex. For example

var temp = price.Replace(".", "<TEMP>");
var temp2 = temp.Replace(",", ".");
var replaced = temp2.Replace("<TEMP>", ",");
Submit answered 4/8, 2010 at 9:48 Comment(0)
H
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.

Hulsey answered 4/8, 2010 at 9:50 Comment(3)
100,00.56 is an English representation, 10.000,56 - Sweden/Russian/etcPreservative
@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.Priestess
fixed dummyChar to be a variable not a literal in the last line.Landonlandor
S
5

Also have a look at

System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator
Sport answered 4/8, 2010 at 9:50 Comment(0)
S
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.

Shoreline answered 4/8, 2010 at 9:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.