Skip navigation

Goal:

A frequently-required function for Arabic accountants who are using Oracle database-based applications.

 

Here is the function for numbers less than 99999

 

CREATE OR replace FUNCTION Writenuminarabicwords (totnetget IN NUMBER)
RETURN VARCHAR2 
IS 
  strnum            VARCHAR2(10); 
  numinarabic       VARCHAR2(100); 
  a7ad              VARCHAR2(1); 
  a7adarabic        VARCHAR2(50); 
  asharat           VARCHAR2(1); 
  asharatarabic     VARCHAR2(50); 
  me2at             VARCHAR2(1); 
  me2atarabic       VARCHAR2(50); 
  alaaf             VARCHAR2(1); 
  alaafarabic       VARCHAR2(50); 
  asharatolof       VARCHAR2(1); 
  asharatolofarabic VARCHAR2(50); 
  me2atolof         VARCHAR2(1); 
  me2atolofarabic   VARCHAR2(50); 
  malayeen          VARCHAR2(1); 
  malayeenarabic    VARCHAR2(50); 
  numlength         NUMBER; 
BEGIN 
    strnum := To_char(totnetget); 

    -- check a7ad digit 
    a7ad := Substr(strnum, Length(strnum), 1); 

    IF a7ad != '0' THEN 
      IF a7ad = '1' THEN 
        a7adarabic := 'واحد'; 
      ELSIF a7ad = '2' THEN 
        a7adarabic := 'اثنان'; 
      ELSIF a7ad = '3' THEN 
        a7adarabic := 'ثلاثة'; 
      ELSIF a7ad = '4' THEN 
        a7adarabic := 'أربعة'; 
      ELSIF a7ad = '5' THEN 
        a7adarabic := 'خمسة'; 
      ELSIF a7ad = '6' THEN 
        a7adarabic := 'ستة'; 
      ELSIF a7ad = '7' THEN 
        a7adarabic := 'سبعة'; 
      ELSIF a7ad = '8' THEN 
        a7adarabic := 'ثمانية'; 
      ELSIF a7ad = '9' THEN 
        a7adarabic := 'تسعة'; 
      END IF; 

      numinarabic := a7adarabic; 
    END IF; 

    -- check asharat digit 
    IF( Length(strnum) > 1 ) THEN 
      asharat := Substr(strnum, Length(strnum) - 1, 1); 

      IF asharat != '0' THEN 
        IF asharat = '1' THEN 
          IF( a7ad = '0' ) THEN 
            asharatarabic := 'عشرة'; 
          ELSE 
            IF a7ad = '1' THEN 
              asharatarabic := 'احد عشر'; 
            ELSIF a7ad = '2' THEN 
              asharatarabic := 'اثنا عشر'; 
            ELSIF a7ad = '3' THEN 
              asharatarabic := 'ثلاثة عشر'; 
            ELSIF a7ad = '4' THEN 
              asharatarabic := 'أربعة عشر'; 
            ELSIF a7ad = '5' THEN 
              asharatarabic := 'خمسة عشر'; 
            ELSIF a7ad = '6' THEN 
              asharatarabic := 'ستة عشر'; 
            ELSIF a7ad = '7' THEN 
              asharatarabic := 'سبعة عشر'; 
            ELSIF a7ad = '8' THEN 
              asharatarabic := 'ثمانية عشر'; 
            ELSIF a7ad = '9' THEN 
              asharatarabic := 'تسعة عشر'; 
            END IF; 
          END IF; 
        ELSIF asharat = '2' THEN 
          asharatarabic := 'عشرون'; 
        ELSIF asharat = '3' THEN 
          asharatarabic := 'ثلاثون'; 
        ELSIF asharat = '4' THEN 
          asharatarabic := 'أربعون'; 
        ELSIF asharat = '5' THEN 
          asharatarabic := 'خمسون'; 
        ELSIF asharat = '6' THEN 
          asharatarabic := 'ستون'; 
        ELSIF asharat = '7' THEN 
          asharatarabic := 'سبعون'; 
        ELSIF asharat = '8' THEN 
          asharatarabic := 'ثمانون'; 
        ELSIF asharat = '9' THEN 
          asharatarabic := 'تسعون'; 
        END IF; 
      ELSE 
        asharatarabic := a7adarabic; 
      END IF; 

      IF( a7ad != '0' 
          AND asharat != '1' ) THEN 
        numinarabic := a7adarabic 
                       || ' و ' 
                       || asharatarabic; 
      ELSE 
        numinarabic := asharatarabic; 
      END IF; 
    END IF; 

    -- check me2at digit 
    IF( Length(strnum) > 2 ) THEN 
      me2at := Substr(strnum, Length(strnum) - 2, 1); 

      IF me2at != '0' THEN 
        IF me2at = '1' THEN 
          me2atarabic := 'مائة'; 
        ELSIF me2at = '2' THEN 
          me2atarabic := 'مائتان'; 
        ELSIF me2at = '3' THEN 
          me2atarabic := 'ثلاثمائة'; 
        ELSIF me2at = '4' THEN 
          me2atarabic := 'أربعمائة'; 
        ELSIF me2at = '5' THEN 
          me2atarabic := 'خمسمائة'; 
        ELSIF me2at = '6' THEN 
          me2atarabic := 'ستمائة'; 
        ELSIF me2at = '7' THEN 
          me2atarabic := 'سبعمائة'; 
        ELSIF me2at = '8' THEN 
          me2atarabic := 'ثمانمائة'; 
        ELSIF me2at = '9' THEN 
          me2atarabic := 'تسعمائة'; 
        END IF; 
      ELSE 
        me2atarabic := asharatarabic; 
      END IF; 

      IF( a7ad != '0' 
           OR asharat != '0' ) THEN 
        numinarabic := me2atarabic 
                       || ' و ' 
                       || Writenuminarabicwords(To_number(Substr(strnum,
                                                          Length(strnum)
                                                          - 1, 2))); 
      ELSE 
        numinarabic := me2atarabic; 
      END IF; 
    END IF; 

    -- alaaf digit 
    IF( Length(strnum) > 3 ) THEN 
      alaaf := Substr(strnum, Length(strnum) - 3, 1); 

      IF alaaf != '0' THEN 
        IF alaaf = '1' THEN 
          alaafarabic := 'ألف'; 
        ELSIF alaaf = '2' THEN 
          alaafarabic := 'ألفان'; 
        ELSIF alaaf = '3' THEN 
          alaafarabic := 'ثلاثة الاف'; 
        ELSIF alaaf = '4' THEN 
          alaafarabic := 'أربعة الاف'; 
        ELSIF alaaf = '5' THEN 
          alaafarabic := 'خمسة الاف'; 
        ELSIF alaaf = '6' THEN 
          alaafarabic := 'ستة الاف'; 
        ELSIF alaaf = '7' THEN 
          alaafarabic := 'سبعة الاف'; 
        ELSIF alaaf = '8' THEN 
          alaafarabic := 'ثمانية الاف'; 
        ELSIF alaaf = '9' THEN 
          alaafarabic := 'تسعة الاف'; 
        END IF; 
      ELSE 
        alaafarabic := me2atarabic; 
      END IF; 

      IF( a7ad != '0' 
           OR asharat != '0' 
           OR me2at != '0' ) THEN 
        numinarabic := alaafarabic 
                       || ' و ' 
                       || Writenuminarabicwords(To_number(Substr(strnum,
                                                          Length(strnum)
                                                          - 2, 3))); 
      ELSE 
        numinarabic := alaafarabic; 
      END IF; 
    END IF; 

    -- asharatolof digit 
    IF( Length(strnum) > 4 ) THEN 
      asharatolof := Substr(strnum, Length(strnum) - 4, 1); 

      IF asharatolof != '0' THEN 
        IF asharatolof = '1' THEN 
          asharatolofarabic := 'عشرة آلاف'; 
        ELSIF asharatolof = '2' THEN 
          asharatolofarabic := 'عشرون ألفا'; 
        ELSIF asharatolof = '3' THEN 
          asharatolofarabic := 'ثلاثون ألفا'; 
        ELSIF asharatolof = '4' THEN 
          asharatolofarabic := 'أربعون ألفا'; 
        ELSIF asharatolof = '5' THEN 
          asharatolofarabic := 'خمسون ألفا'; 
        ELSIF asharatolof = '6' THEN 
          asharatolofarabic := 'ستون ألفا'; 
        ELSIF asharatolof = '7' THEN 
          asharatolofarabic := 'سبعون ألفا'; 
        ELSIF asharatolof = '8' THEN 
          asharatolofarabic := 'ثمانون ألفا'; 
        ELSIF asharatolof = '9' THEN 
          asharatolofarabic := 'تسعون ألفا'; 
        END IF; 
      ELSE 
        asharatolofarabic := alaafarabic; 
      END IF; 

      IF( a7ad != '0' 
           OR asharat != '0' 
           OR me2at != '0' 
           OR alaaf != '0' ) THEN 
        numinarabic := Writenuminarabicwords(To_number(Substr(strnum, Length(
                                                       strnum) 
                                                                      - 4, 2)))
                       || CASE 
                            WHEN ( asharatolof = '1' 
                                   AND alaaf = '0' ) THEN ' آلاف ' 
                            ELSE ' ألف ' 
                          END 
                       || CASE 
                            WHEN ( a7ad != '0' 
                                    OR asharat != '0' 
                                    OR me2at != '0' ) THEN ' و ' 
                          END 
                       || Writenuminarabicwords(To_number(Substr(strnum,
                                                          Length(strnum)
                                                          - 2, 3))); 
      ELSE 
        numinarabic := asharatolofarabic; 
      END IF; 
    END IF; 

    -- me2atolof digit 
    IF( Length(strnum) > 5 ) THEN 
      me2atolof := Substr(strnum, Length(strnum) - 5, 1); 

      IF me2atolof != '0' THEN 
        IF me2atolof = '1' THEN 
          me2atolofarabic := 'مائة ألف'; 
        ELSIF me2atolof = '2' THEN 
          me2atolofarabic := 'مائتا ألف'; 
        ELSIF me2atolof = '3' THEN 
          me2atolofarabic := 'ثلاثمائة ألف'; 
        ELSIF me2atolof = '4' THEN 
          me2atolofarabic := 'أربعمائة ألف'; 
        ELSIF me2atolof = '5' THEN 
          me2atolofarabic := 'خمسمائة ألف'; 
        ELSIF me2atolof = '6' THEN 
          me2atolofarabic := 'ستمائة ألف'; 
        ELSIF me2atolof = '7' THEN 
          me2atolofarabic := 'سبعمائة ألف'; 
        ELSIF me2atolof = '8' THEN 
          me2atolofarabic := 'ثمانمائة ألف'; 
        ELSIF me2atolof = '9' THEN 
          me2atolofarabic := 'تسعمائة ألف'; 
        END IF; 
      ELSE 
        me2atolofarabic := asharatolofarabic; 
      END IF; 

      IF( a7ad != '0' 
           OR asharat != '0' 
           OR me2at != '0' 
           OR alaaf != '0' 
           OR asharatolof != '0' ) THEN 
        numinarabic := Writenuminarabicwords(To_number(Substr(strnum, Length(
                                                       strnum) 
                                                                      - 5, 3)))
                       || ' ألفا و ' 
                       || Writenuminarabicwords(To_number(Substr(strnum,
                                                          Length(strnum)
                                                          - 2, 3))); 
      ELSE 
        numinarabic := me2atolofarabic; 
      END IF; 
    END IF; 

    -- more than 999999 is not available in this version 
    IF( Length(strnum) > 6 ) THEN 
      numinarabic := strnum; 
    END IF; 

    RETURN numinarabic; 
END; 

I hope it helps someone.

 

Regards

Mahmoud