Skip to content

Instantly share code, notes, and snippets.

@insideone
Created May 15, 2016 13:45
Show Gist options
  • Save insideone/2c4172e6023a2b9ac07a0dbec61f4373 to your computer and use it in GitHub Desktop.
Save insideone/2c4172e6023a2b9ac07a0dbec61f4373 to your computer and use it in GitHub Desktop.
C#: Склонение существительного после числительного
int i = 3;
Console.WriteLine(i.Decline("Прошёл", "Прошло", "Прошло") + " " + i.ToString() + " " + i.Decline("час", "часа", "часов"));
static class IntegerExtend
{
/// <summary>
/// Склоняет существительное в зависимости от числительного идущего перед ним.
/// </summary>
/// <param name="num">Число идущее перед существительным.</param>
/// <param name="normative">Именительный падеж слова.</param>
/// <param name="singular">Родительный падеж ед. число.</param>
/// <param name="plural">Множественное число.</param>
public static string Decline(this int num, string nominative, string singular, string plural)
{
if (num > 10 && ((num % 100) / 10) == 1) return plural;
switch (num % 10)
{
case 1:
return nominative;
case 2:
case 3:
case 4:
return singular;
default: // case 0, 5-9
return plural;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment