-3.3 C
New York
Wednesday, January 8, 2025

ios – convert quantity to phrases name tafqit in arabic


I need to create a swift utility that converts, in Arabic, numbers to phrases. For instance, as in https://tafqeet.com
with forex title

Instance: 100.5 can be ” مائة دولار أمريكي و خمسون سنت لا غير”, which might translate to “100 US {Dollars} and Fifty Cents”

I already understand how to do that for English. Right here’s the Swift code I exploit for English number-to-word conversion:

func numberToWords(_ num: Int) -> String {
    let items = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
    let teenagers = ["", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
    let tens = ["", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
    let 1000's = ["", "Thousand", "Million", "Billion"]

    if num == 0 {
        return "Zero"
    }

    var num = num
    var outcome = ""
    var thousandCounter = 0

    whereas num > 0 {
        if num % 1000 != 0 {
            outcome = helper(num % 1000) + 1000's[thousandCounter] + " " + outcome
        }
        num /= 1000
        thousandCounter += 1
    }

    return outcome.trimmingCharacters(in: .whitespaces)
}

non-public func helper(_ num: Int) -> String {
    let items = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
    let teenagers = ["", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
    let tens = ["", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]

    if num == 0 { return "" }
    if num < 10 { return items[num] + " " }
    if num < 20 { return teenagers[num - 10] + " " }
    if num < 100 { return tens[num / 10] + " " + helper(num % 10) }
    return items[num / 100] + " Hundred " + helper(num % 100)
}

This works effectively for English, however Arabic has extra advanced grammar guidelines and codecs for numbers. I’m conscious of instruments like `NSNumberFormatter`, however it doesn’t straight help Arabic number-to-word conversion.

How can I implement number-to-word conversion in Arabic in Swift, contemplating:
    •   The grammar guidelines of Arabic numbers.
    •   The help for fractional numbers like 105.75.

Any assist, strategies, or examples can be significantly appreciated!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles