Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Format vs ToString and using InvariantCulture

Tags:

c#

I am a little confused here.

What should I use

Console.WriteLine((val/1085).ToString("N"));

VS

Console.WriteLine(String.Format("{0:N}", (val/1085)));

Also how do I fit the InvariantCulture? ANY BEST PRACTICES :)?

like image 735
Joe Avatar asked Jul 29 '10 09:07

Joe


Video Answer


2 Answers

Actually I prefer a third form:

Console.WriteLine("{0:N}", val / 1085);

Console.WriteLine can do the String.Format for you.

Console.WriteLine does not allow you to supply a culture. If that is what you want, you will still have to use String.Format. As in:

String.Format(CultureInfo.InvariantCulture, "{0:N}", 123456789);

I do not recommend using that because international users will have trouble reading that. To me 123,456,789.00 looks strange.

like image 180
heijp06 Avatar answered Sep 27 '22 17:09

heijp06


For formatting + culture I prefer:

 .ToString("####0.00",CultureInfo.InvariantCulture)

or

.ToString("N",CultureInfo.InvariantCulture)
like image 35
Julian de Wit Avatar answered Sep 27 '22 16:09

Julian de Wit