Created
December 7, 2009 16:12
-
-
Save subdigital/250895 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static string Pluralize(this string str) | |
{ | |
if(str.EndsWith("s")) | |
return str + "es"; | |
return str + "s"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static string Quantify(this string str, double count) | |
{ | |
if(count == 1.0) | |
return str; | |
return str.Pluralize(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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