Search code examples
c#.netstringformattingdata-conversion

String.Format vs ToString and using InvariantCulture


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 :)?


Solution

  • 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.