2

As I saw in google, there are many implementation for string to an int or decimal... but not to a string.

For example:

decimal d1 = 32221.0210m;
Console.WriteLine(d1.ToString("#,###.00"));

This works but the problem is that I have a string and I don't want to cast it to a numeric.

string str = "32221.0210";

I want it to be:

str = "32,221.02";

Is it possible (without casting)?

For those who ask if is there any reason why not cast: Yes. I am using Infrastructures that receive a string. The infrastructures team don't want to convert it to decimal because they also may recieve another values such as date, etc... In worse case, it is possible to TryParse decimal, so we know for sure it will work. But for now, we are trying to parse a string to a string as written above.

Thanks in advance :)

4
  • Is there any special reason to not using conversation? Commented Feb 5, 2014 at 7:23
  • Did you try string.Format() (msdn.microsoft.com/en-us/library/…)?
    – Johnny
    Commented Feb 5, 2014 at 7:23
  • 1
    Why do you not want to cast it to a numeric value?
    – Richard Ev
    Commented Feb 5, 2014 at 7:27
  • @RichardEv I have updated my post, there is an answer to your question. Commented Feb 5, 2014 at 7:34

1 Answer 1

1

A temporary var of type decimal is required:

decimal d;
string str = decimal.TryParse("32221.0210", out d)? string.Format("{0:#,##0.00}", d):string.Empty; 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.