Created
April 23, 2018 14:12
-
-
Save Dok11/e6765e9f13ba99bc901a82a0ca9452e1 to your computer and use it in GitHub Desktop.
Angular. Get plural code (1,2,5)
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
/** | |
* Метод возвращает нужный код числительного для разных цифр. | |
* Возможные коды ответа: 1, 2, 5 | |
* Например, 1 - для 1 товар, 2 - товара, 5 - товаров | |
*/ | |
public getPluralCode(n: number): number { | |
return (n % 10 === 1) && (n % 100 !== 11) | |
? 1 | |
: (n % 10 >= 2) | |
&& (n % 10 <= 4) | |
&& ((n % 100 < 10) || (n % 100 >= 20)) ? 2 : 5; | |
} |
а как быть если для некоторых целей придётся делать 01, 02, 03, 04, 05, 06, 07, 08, 09?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
это будет работать как с 1, 2, 3, 4, 5, 6, 7, 8 так и с 21, 32, 43, 54, 65, 76, 87, 98?