C# String Formats


/ Published in: C#
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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

You need to login to post a comment.