Created
January 11, 2013 11:36
-
-
Save vadimii/4510096 to your computer and use it in GitHub Desktop.
Numbers with word cases
This file contains hidden or 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 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