Skip to content

Instantly share code, notes, and snippets.

@subdigital
Created December 7, 2009 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subdigital/250895 to your computer and use it in GitHub Desktop.
Save subdigital/250895 to your computer and use it in GitHub Desktop.
public static string Pluralize(this string str)
{
if(str.EndsWith("s"))
return str + "es";
return str + "s";
}
public static string Quantify(this string str, double count)
{
if(count == 1.0)
return str;
return str.Pluralize();
}
public static string Format(this TimeSpan span)
{
double number;
string unit;
if (span.Days > 0)
{
number = span.TotalDays;
unit = "day";
}
else if (span.Hours > 0)
{
number = span.TotalHours;
unit = "hour";
}
else if (span.Minutes > 0)
{
number = span.TotalMinutes;
unit = "minute";
}
else if (span.Seconds > 0)
{
number = span.TotalSeconds;
unit = "second";
}
else
{
number = span.TotalMilliseconds;
unit = "millisecond";
}
return string.Format("{0:0.#} {1}", number, unit.Quantify(number));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment