0

I get from a webservice the following strings:

12.95

or

1,200.99

Is there an option to convert these values to the following values without manipulating the string?

12,95

or

1200,99

I tried it with some Culture options but didn't get it right...

EDIT

I tried this:

    //return string.Format( "{0:f2}", Convert.ToDecimal( price ) );
    //return string.Format(CultureInfo.GetCultureInfo("de-de"), "{0:0}", price);

                

    NumberFormatInfo format = new System.Globalization.NumberFormatInfo();

    format.CurrencyDecimalDigits = 2;
    format.CurrencyDecimalSeparator = ",";
    format.CurrencyGroupSeparator = "";

    return decimal.Parse(price).ToString(format);
Community
  • 1
  • 1
tronc
  • 683
  • 4
  • 12
  • 23

5 Answers5

7
var input = "1,200.99";

//Convert to decimal using US culture (or other culture using . as decimal separator)
decimal value = decimal.Parse(input, CultureInfo.GetCultureInfo("en-US"));

//Convert to string using DE culture (or other culture using , as decimal separator)
string output = value.ToString(CultureInfo.GetCultureInfo("de-DE"));

Console.WriteLine(output); //1200,99
Meta-Knight
  • 17,626
  • 1
  • 48
  • 58
  • exactly what I was looking for, thanks to you and all others! missing clue was adding the culture to the input string. – tronc Nov 13 '13 at 14:15
1

What about something like this:

    double number;
    double.TryParse("1,200.99", NumberStyles.Any, CultureInfo.CreateSpecificCulture("en-US"), out number);
    var formattedNumber = number.ToString(CultureInfo.CreateSpecificCulture("de-DE"));

Then return or write out formattedNumber (whatever you need to do).

Joe Ratzer
  • 18,176
  • 3
  • 37
  • 51
  • 1
    input: 4.95, number is then 495.0 and formattedNumber 495 ... my brain hurts ;) – tronc Nov 13 '13 at 14:11
  • The above code example assumes your machine has the same culture as the input, otherwise specify it in the TryParse method - http://msdn.microsoft.com/en-us/library/3s27fasw(v=vs.110).aspx – Joe Ratzer Nov 13 '13 at 14:14
  • 1
    Given your problem with the input culture, I've updated the answer in case it helps others... – Joe Ratzer Nov 13 '13 at 14:42
0

Yes and no. First, what you have is a string, and so you cannot change the formatting of it as you're attempting to. However, to achieve what you would like, you can parse the string into a decimal value and then use the formatting options for decimals to display it in any reasonable way.

Andrew Ring
  • 3,185
  • 1
  • 23
  • 28
0

You may try for something like this:

String.Format("{0:#,###0}", 0);

or may be like this:

string str = yourNumber.Remove(",").Replace(".",",");
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

Close enough tronc,

Try this snippet:

String curStr = "12.95";
Decimal decVal;
var valid = Decimal.TryParse(curStr, out decVal);
if (!valid) throw new Exception("Invalid format.");
String newFormat = decVal.ToString("C", System.Globalization.CultureInfo.CreateSpecificCulture("de-DE"));

Within the toString(...) call, you can append a number after 'C' to specify how many decimal places should follow. E.g "C3".

brendonofficial
  • 850
  • 8
  • 10