War with robots: declension of nouns after numerals

    Pocket robotIn the first part of the Terminator, Rhys Kyle talks about how cool robots have learned to disguise themselves as humans. That now they have real skin and hair, they sweat, etc. About the same models, he said that it was easy to distinguish them by rubber skin.

    I think that even earlier models differed quite simply - they said: “The soldier Chris Katarn killed 10 enemies, spent 342 rounds, received 0 wounds,” etc., which they immediately fired at.

    Indeed, so far, despite the development of the web, on many sites you can find “50 users”, “1 comments”, “0 posts”, etc. But how much more pleasant it is when the site speaks human language to you and correctly conjugates it words by numbers.

    And after all, to do this is not difficult at all. Below are simple ready-made functions that can solve this problem in PHP and Javascript. They are so simple that it is not difficult to transfer them to any other language.

    The function passes the number of entities for which you want to select endings, and an array of words (or endings for words) for numbers 1, 4 and 5. For example ['oyster', 'oysters', 'oysters'].

    Php



    1. / **
    2.  * The function returns the ending for the plural of a word based on the number and array of endings
    3.  * param  $ number Integer The number based on which you want to form the ending
    4.  * param  $ endingsArray Array An array of words or endings for numbers (1, 4, 5),
    5.  * e.g. array ('apple', 'apples', 'apples')
    6.  * return String
    7.  * /
    8. function getNumEnding ($ number, $ endingArray)
    9. {
    10.     $ number = $ number% 100;
    11.     if ($ number> = 11 && $ number <= 19) {
    12.         $ ending = $ endingArray [2];
    13.     }
    14.     else {
    15.         $ i = $ number% 10;
    16.         switch ($ i)
    17.         {
    18.             case (1): $ ending = $ endingArray [0]; break;
    19.             case (2):
    20.             case (3):
    21.             case (4): $ ending = $ endingArray [1]; break;
    22.             default : $ ending = $ endingArray [2];
    23.         }
    24.     }
    25.     return $ ending;
    26. }


    Javascript


    1. / **
    2.  * The function returns the ending for the plural of a word based on the number and array of endings
    3.  * param  iNumber Integer The number based on which you want to form the ending
    4.  * param  aEndings Array An array of words or endings for numbers (1, 4, 5),
    5.  * for example ['apple', 'apples', 'apples']
    6.  * return String
    7.  * /
    8. function getNumEnding (iNumber, aEndings)
    9. {
    10.     var sEnding, i;
    11.     iNumber = iNumber% 100;
    12.     if (iNumber> = 11 && iNumber <= 19) {
    13.         sEnding = aEndings [2];
    14.     }
    15.     else {
    16.         i = iNumber% 10;
    17.         switch (i)
    18.         {
    19.             case (1): sEnding = aEndings [0]; break ;
    20.             case (2):
    21.             case (3):
    22.             case (4): sEnding = aEndings [1]; break ;
    23.             default : sEnding = aEndings [2];
    24.         }
    25.     }
    26.     return sEnding;
    27. }


    Do not forget to handle the case separately, for the number 0. Just writing, for example, “0 records” is not enough. At a minimum, you need to write “No Records” or change the design by hiding the generally empty block with records.

    UPD: Thanks to IGlukhov for correcting an illiterate name!

    Also popular now: