androidkotlinnumber-formattingfarsinumber-systems

How can I convert Persian (Farsi) and Arabic numbers to English numbers in Kotlin?


I want to pass a date as string to a web service. When I get the date in devices that use Persian, localized digits are Persian and the server can't convert this string to DateTime. So I need to convert all digits to English.

The date I receive in devices with a Persian locale:

"۲۰۲۰/۰۸/۲۱"

And these are Persian (Farsi) digits:

(۰ -> 0)  (۱ -> 1) (۲ -> 2) (۳ -> 3) (۴ -> 4) (۵ -> 5) (۶ -> 6) (۷-> 7) (۸ -> 8) (۹ -> 9)

I need a function in Kotlin to perform this.


Solution

  • Finally bellow function solved my problem:

    fun PersianToEnglish(persianStr: String):String {
                var result = ""
                var en = '0'
                for (ch in persianStr) {
                    en = ch
                    when (ch) {
                        '۰' -> en = '0'
                        '۱' -> en = '1'
                        '۲' -> en = '2'
                        '۳' -> en = '3'
                        '۴' -> en = '4'
                        '۵' -> en = '5'
                        '۶' -> en = '6'
                        '۷' -> en = '7'
                        '۸' -> en = '8'
                        '۹' -> en = '9'
                    }
                    result = "${result}$en"
                }
                return result
            }