tags:

views:

188

answers:

7

I am working on ASP.NET (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.

A: 

You can do this even without regex. For example

var temp = price.Replace(".", "<TEMP>");
var temp2 = temp.Replace(",", ".");
var replaced = temp2.Replace("<TEMP>", ",");
sukru
+1  A: 

mohsinjk - i know you ask about the regex. just a thought on another tack, would setting your culture not help out here??

jim
+17  A: 

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
Virtual +1, sadly I'm out of votes today. But know you earned it. :)
sarnold
great minds.. snap +1 :-)
jim
+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
+1. Clean and small.
Yves M.
+2  A: 

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.

scripni
`100,00.56` is an English representation, `10.000,56` - Sweden/Russian/etc
abatishchev
@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
@Alan: You're right, didn't mentioned right away
abatishchev
@scripni - fixed dummyChar to be a variable not a literal in the last line.
ck
+2  A: 

Also have a look at

System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator
Grozz
A: 

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.

lasseespeholt
+1  A: 

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(ru) :
        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