easy way to format numbers to strings?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BobLewiston
    New Member
    • Feb 2009
    • 93

    easy way to format numbers to strings?

    In C#, I'd like to convert some numbers to strings in a standard format, but preferably in a simple way, such as if you were going to output them rather than store them in string variables, a la:
    Code:
    Console.WriteLine ("{0:F2}", num);
    rather than use a bunch or string methods to make them all the same number of significant digits. Isn't there any way to do this?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Well there is the .ToString() function on every data type

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Expanding on what Plater said... .ToString() has some overrides that allow you to use nearly all the same format commands as what you included in your question.

      Code:
      float bob = 69.486;
      string strBob = bob.ToString("{0:F2}");
      Should work. Might need a little massaging; its been a while since I've done that.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        I think its just .ToString("F2") ;

        Comment

        Working...