Search code examples
javascriptnumbersarabic

How to Convert Numbers (from 1 to 99) to the Equivalent Arabic Ordinal Number String


Convert Numbers (from 1 to 99) to Arabic Ordinal Number String

Unlike English Ordinal Numbers where the ordinal number is made from the original number (in digital form) and a suffix is added to it (i.e. 1st, 2nd, 3rd, 4th......), the Arabic Ordinal Numbers cannot be written like that and an Arabic Ordinal Number is a full-text representation.

The table below lists selected Arabic Ordinal Numbers (from 1 to 99).

Arabic Ordinal Numbers differ if the subject is Masculine or Feminine.

There is no Javascript function or a solution on StackOverflow that can be found that can convert a number (in the range from 1 to 99) to the equivalent Arabic Ordinal Number text taking into account the type of the subject (Masculine or Feminine).

An example of such a function could be:

ordinalsAr(5);
ordinalsAr(21);

// result ==> الخامس
// result ==> الحادي والعشرون

ordinalsAr(5, true);   // true flag for Feminine
ordinalsAr(21, true);  // true flag for Feminine

// result ==> الخامسة
// result ==> الحادية والعشرون

Arabic Ordinal Numbers
English Ordinals English Ordinals Arabic Ordinals Arabic Ordinals
 Ordinal Number Masculine Feminine
1st First الأول الأولى
2nd Second الثاني الثانية
3rd Third الثالث الثالثة
4th Fourth الرابع الرابعة
5th Fifth الخامس الخامسة
6th Sixth السادس السادسة
7th Seventh السابع السابعة
8th Eighth الثامن الثامنة
9th Ninth التاسع التاسعة
10th Tenth العاشر العاشرة
11th Eleventh الحادي عشر الحادية عشرة
12th Twelfth الثاني عشر الثانية عشرة
13th Thirteenth الثالث عشر الثالثة عشرة
14th Fourteenth الرابع عشر الرابعة عشرة
15th Fifteenth الخامس عشر الخامسة عشرة
16th Sixteenth السادس عشر السادسة عشرة
17th Seventeenth السابع عشر السابعة عشرة
18th Eighteenth الثامن عشر الثامنة عشرة
19th Nineteenth التاسع عشر التاسعة عشرة
20th Twentieth العشرون العشرون
21st Twenty-first الحادي والعشرون الحادية والعشرون
22nd Twenty-second الثاني والعشرون الثانية والعشرون
23rd Twenty-third الثالث والعشرون الثالثة والعشرون
24th Twenty-fourth الرابع والعشرون الرابعة والعشرون
…. …………. …………. ………….
29th Twenty-ninth التاسع والعشرون التاسعة والعشرون
30th Thirtieth الثلاثون الثلاثون
…. …………. …………. ………….
40th Fortieth الأربعون الأربعون
…. …………. …………. ………….
99th Ninety-Ninth التاسع والتسعون التاسعة والتسعون

enter image description here


Solution

  • Looking at Alan Omar's code concept, a shorter code to Convert Numbers (from 1 to 99) to Arabic Ordinal String is as follows.

    More information about Arabic Numbers conversion can be found here Arabic Numbers

    /*********************************************************************
    * @function      : ordinalsAr(number [, isFeminine])
    * @purpose       : Converts numbers from 1 to 99 to Arabic Ordinal String
    * @version       : 1.00
    * @author        : Mohsen Alyafei
    * @date          : 27 Jan 2022
    * @Licence       : MIT
    * @param         : {number} Integer from 1 to 99
    * @param         : [isFeminine] the subject's gender:
    *                  false (default) --> Masculine subject (e.g. output الأول الثاني الثالث)
    *                  true            --> Feminine subject  (e.g. output الأولى الثانية الثالثة)
    * @returns       : {string} The ordinal Arabic Text.
    **********************************************************************/
    
    
    function ordinalsAr(num, isFeminine=false) {
    num %= 100;                  // only handle the lowest 2 digits (1-99) ignore others
    let    the = "ال",           // set this to "" if you don't want the output prefixed with letters "ال"
          unit = num % 10,
        ordinal= the + [,"أول","ثاني","ثالث","رابع","خامس","سادس","سابع","ثامن","تاسع","عاشر"]
                       [num === 10 ? num : unit],                       // letters for lower part of ordinal string
        female = isFeminine ? "ة" : "",                                 // add letter "ة" if Feminine
          ones = (unit === 1 ? the + "حادي" : ordinal) + female;        // special cases for 21, 31, 41, etc.
    return num <11 ? ordinal + (isFeminine && num ===1 ? "ى" : female): // from 1 to 10
           num <20 ? ones + " عشر" + female :                           // from 11 to 19
           (unit ? ones + " و" : "") +                                  // else 20 to 99
           "ال" +                                                       // always add "ال"
           [,,"عشر","ثلاث","أربع","خمس","ست","سبع","ثمان","تسع"]       // letters for 20, 30, 40...
           [ ~~(num / 10)] + "ون";
    }
    // *=======================================================
    
    
    //=======================================
    //             Test Cases
    //=======================================
    // List the ordinal numbers from 1 to 99
    // in both Masculine and Feminine genders
    //=======================================
    for (let i=1; i<100; i++) {
    console.log(i,"M:  "+ordinalsAr(i),
                  "F:  "+ordinalsAr(i,true) );
    }