Posted By

stewshack on 05/16/09


Tagged

format String c


Versions (?)

Who likes this?

11 people have marked this snippet as a favorite

danfinrud55
SnipItPrpDemo
poldaker
paelgr
rodrigojuarez
khouser
markstahler
blackf0rk
tonicglop
jalalx
martyncoup


C# String Formats


 / Published in: C#
 

Here are some example string formats. As I discover more I will add them here.

  1. int MySalary = 123456;
  2.  
  3. String.Format( "{0:C}", MySalary ); // $123,456.00
  4. String.Format( "{0:D}", MySalary ); // 123456
  5. String.Format( "{0:E}", MySalary ); // 1.234560E+005
  6. String.Format( "{0:F}", MySalary ); // 123456.00
  7. String.Format( "{0:G}", MySalary ); // 123456
  8. String.Format( "{0:N}", MySalary ); // 123,456.00
  9. String.Format( "{0:X}", MySalary ); // 1E240
  10. String.Format( "{0:#%}", MySalary ); // 12345600%
  11. String.Format( "{0:#,###}", MySalary ); // 123,456
  12. String.Format( "{0:#,###.00}", MySalary ); // 123,456.00
  13. String.Format( "{0:#,###.##}", MySalary ); // 123,456
  14. String.Format( "{0:'$'#,###.00}", MySalary ); // $123,456.00
  15. String.Format( "{0:#,###' lbs'}", MySalary ); // 123,456 lbs
  16.  
  17. MySalary = -123456;
  18. String.Format( "{0:#0}", MySalary ); // -123456
  19. String.Format( "{0:#0;(#0)}", MySalary ); // (123456)
  20.  
  21. DateTime.Now.ToString( "yyyy-MM-ddTHH:MM:ss" ); // XSD:DateTime format 2009-05-16T10:05:46

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: freshclickmedia on June 17, 2010

Hi stewshack - have you visited http://blog.stevex.net/string-formatting-in-csharp/ ?

Posted By: stewshack on June 24, 2010

Yeah, that's a great resource. John Skeet just released on too. http://john-sheehan.com/blog/wp-content/uploads/msnet-formatting-strings.pdf

You need to login to post a comment.