B4A Library [B4X] Arabic Numbers To Words

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
Pure b4x code:
B4X:
Sub NumberToArabicLetters(number As Double) As String
    Dim ones(10) As String
    Dim tens(10) As String
    Dim hundreds(10) As String
    Dim thousands(10) As String
    Dim result As String = ""
    
    Try
    
        ones(0) = ""
        ones(1) = "واحد"
        ones(2) = "اثنان"
        ones(3) = "ثلاثة"
        ones(4) = "أربعة"
        ones(5) = "خمسة"
        ones(6) = "ستة"
        ones(7) = "سبعة"
        ones(8) = "ثمانية"
        ones(9) = "تسعة"

        tens(0) = ""
        tens(1) = "عشرة"
        tens(2) = "عشرون"
        tens(3) = "ثلاثون"
        tens(4) = "أربعون"
        tens(5) = "خمسون"
        tens(6) = "ستون"
        tens(7) = "سبعون"
        tens(8) = "ثمانون"
        tens(9) = "تسعون"

        hundreds(0) = ""
        hundreds(1) = "مائة"
        hundreds(2) = "مائتان"
        hundreds(3) = "ثلاثمائة"
        hundreds(4) = "أربعمائة"
        hundreds(5) = "خمسمائة"
        hundreds(6) = "ستمائة"
        hundreds(7) = "سبعمائة"
        hundreds(8) = "ثمانمائة"
        hundreds(9) = "تسعمائة"

        thousands(0) = ""
        thousands(1) = "ألف"
        thousands(2) = "ألفان"
        thousands(3) = "ثلاثة آلاف"
        thousands(4) = "اربعة آلاف"
        thousands(5) = "خمسة آلاف"
        thousands(6) = "ستة آلاف"
        thousands(7) = "سبعة آلاف"
        thousands(8) = "ثمانية آلاف"
        thousands(9) = "تسعة آلاف"

        If number = 0 Then
            Return "صفر"
        End If

        If number < 0 Then
            result = "سالب "
            number = -number
        End If

        Dim onesDigit As Int = number Mod 10
        Dim tensDigit As Int = (number / 10) Mod 10
        Dim hundredsDigit As Int = (number / 100) Mod 10
        Dim thousandsDigit As Int = (number / 1000) Mod 10

        If thousandsDigit > 0 Then
            result = thousands(thousandsDigit) & " "
            If (hundredsDigit > 0 Or tensDigit > 0 Or onesDigit > 0) Then
                result = result & " و "
            End If
        End If

        If hundredsDigit > 0 Then
            result = result & hundreds(hundredsDigit) & " "
            If (tensDigit > 0 Or onesDigit > 0) Then
                result = result & " و "
            End If
        End If

        If tensDigit > 0 Then
            If tensDigit = 1 Then
                result = result & tens(tensDigit) & " "
                'onesDigit = onesDigit + 10
                If (onesDigit > 0) Then
                    result = result & " و "
                End If
            Else
                result = result & tens(tensDigit) & " "
                If (onesDigit > 0) Then
                    result = result & " و "
                End If
            End If
        End If

        If onesDigit > 0 Then
            result = result & ones(onesDigit)
        End If

    Catch
        Log(LastException)
    End Try
    
    Return result
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…