Monday, October 26, 2009

Code-behind Number Format Reference

This post is to save me some time looking up the format reference for some of the things I find myself looking up every once in a while.

Formatting numbers for display like minutes and seconds to have the preceding zero when the number is less than ten (10) as you would see on a digital clock or stopwatch display.

example: [3:07:10]

   1: ' format integers to have preceding zero if 
   2: ' integer is less than 10 (as in a digital
   3: ' clock or stopwatch display)
   4: ' tsElapsed is a TimeSpan
   5: ' lblElapsedTime is a WPF Label
   6: lblElapsedTime.Content = _
   7:     String.Format("{0}:{1}:{2}", _
   8:     tsElapsed.Hours, _
   9:     tsElapsed.Minutes.ToString("0#"), _
  10:     tsElapsed.Seconds.ToString("0#"))

No comments: