﻿Option Explicit

Private mONES As Variant
Private mTENS As Variant
Private mHUND As Variant
Private mSG As Variant
Private mDL As Variant
Private mPL As Variant
Private mReady As Boolean

Private Sub TafqeetInit()
    If mReady Then Exit Sub
    mONES = Split("|واحد|اثنان|ثلاثة|أربعة|خمسة|ستة|سبعة|ثمانية|تسعة|عشرة|أحد عشر|اثنا عشر|ثلاثة عشر|أربعة عشر|خمسة عشر|ستة عشر|سبعة عشر|ثمانية عشر|تسعة عشر", "|")
    mTENS = Split("||عشرون|ثلاثون|أربعون|خمسون|ستون|سبعون|ثمانون|تسعون", "|")
    mHUND = Split("|مائة|مائتان|ثلاثمائة|أربعمائة|خمسمائة|ستمائة|سبعمائة|ثمانمائة|تسعمائة", "|")
    mSG = Split("ألف|مليون|مليار", "|")
    mDL = Split("ألفان|مليونان|ملياران", "|")
    mPL = Split("آلاف|ملايين|مليارات", "|")
    mReady = True
End Sub

Private Function ThreeWords(ByVal n As Long) As String
    Dim h As Long
    Dim r As Long
    Dim p As String
    Dim w As String
    h = n \ 100
    r = n Mod 100
    p = ""
    If h > 0 Then p = mHUND(h)
    If r > 0 Then
        If r < 20 Then
            w = mONES(r)
        ElseIf (r Mod 10) = 0 Then
            w = mTENS(r \ 10)
        Else
            w = mONES(r Mod 10) & " و" & mTENS(r \ 10)
        End If
        If p <> "" Then p = p & " و" & w Else p = w
    End If
    ThreeWords = p
End Function

Private Function NumWords(ByVal n As Double) As String
    Dim g(3) As Long
    Dim i As Long
    Dim cnt As Long
    Dim v As Long
    Dim t As Double
    Dim out As String
    Dim w As String
    t = n
    cnt = 0
    Do While t >= 1
        g(cnt) = CLng(t - Fix(t / 1000) * 1000)
        t = Fix(t / 1000)
        cnt = cnt + 1
    Loop
    out = ""
    For i = cnt - 1 To 0 Step -1
        v = g(i)
        If v > 0 Then
            If i = 0 Then
                w = ThreeWords(v)
            ElseIf v = 1 Then
                w = mSG(i - 1)
            ElseIf v = 2 Then
                w = mDL(i - 1)
            ElseIf v <= 10 Then
                w = ThreeWords(v) & " " & mPL(i - 1)
            Else
                w = ThreeWords(v) & " " & mSG(i - 1)
            End If
            If out <> "" Then out = out & " و" & w Else out = w
        End If
    Next i
    If out = "" Then out = "صفر"
    NumWords = out
End Function

Private Function NounForm(ByVal k As Double, ByVal sg As String, ByVal dl As String, ByVal pl As String) As String
    Dim r As Long
    r = CLng(k - Fix(k / 100) * 100)
    If r = 1 Then
        NounForm = sg
    ElseIf r = 2 Then
        NounForm = dl
    ElseIf r >= 3 And r <= 10 Then
        NounForm = pl
    Else
        NounForm = sg
    End If
End Function

Function Tafqeet(ByVal n As Double, Optional ByVal cur As String, Optional ByVal curDual As String, Optional ByVal curPlural As String, Optional ByVal subCur As String, Optional ByVal subDual As String, Optional ByVal subPlural As String) As String
    Dim whole As Double
    Dim frac As Long
    Dim s As String
    TafqeetInit
    If cur = "" Then cur = "جنيه"
    If curDual = "" Then curDual = "جنيهان"
    If curPlural = "" Then curPlural = "جنيهات"
    If subCur = "" Then subCur = "قرش"
    If subDual = "" Then subDual = "قرشان"
    If subPlural = "" Then subPlural = "قروش"
    If n < 0 Or n >= 1000000000000# Then
        Tafqeet = "خارج النطاق"
        Exit Function
    End If
    whole = Fix(n)
    frac = CLng(Fix((n - whole) * 100 + 0.5))
    If frac = 100 Then
        whole = whole + 1
        frac = 0
    End If
    If whole = 0 Then
        s = "فقط صفر " & cur
    Else
        s = "فقط " & NumWords(whole) & " " & NounForm(whole, cur, curDual, curPlural)
    End If
    If frac > 0 Then
        s = s & " و" & NumWords(CDbl(frac)) & " " & NounForm(CDbl(frac), subCur, subDual, subPlural)
    End If
    Tafqeet = s & " لا غير"
End Function

