Skip to content

Instantly share code, notes, and snippets.

@vadimii
Created January 11, 2013 11:36
Show Gist options
  • Save vadimii/4510096 to your computer and use it in GitHub Desktop.
Save vadimii/4510096 to your computer and use it in GitHub Desktop.
Numbers with word cases
public static string Case(
this int val,
string one,
string two,
string five) {
var t = (val % 100 > 20) ? val % 10 : val % 20;
switch (t) {
case 1:
return one;
case 2:
case 3:
case 4:
return two;
default:
return five;
}
}
public static string CaseWithValue(
this int val,
string one,
string two,
string five) {
return string.Format("{0} {1}", val, val.Case(one, two, five));
}
public static int Age(this DateTime from) {
var now = DateTime.Now;
return now.Year - from.Year -
(now.Month < from.Month ? 1 :
now.Month == from.Month && now.Day < from.Day ? 1 : 0);
}
birthday.Value.Age().CaseWithValue("год", "года", "лет");
// 16 лет
// 23 года
// 21 год
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment