Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android convert Arabic number to English Number

I get the following error from the gps:

Fatal Exception: java.lang.NumberFormatException
Invalid double: "-٣٣٫٩٣٨٧٤"

Now this is from a error that I got from a user via Fabric. It looks like arabic so I'm guessing it only happens if you have the language set to that, or your sim card? Is it possible to force the gps to send characters in the 0-9 range? Or can I somehow fix this?

like image 529
rosu alin Avatar asked Sep 08 '16 08:09

rosu alin


People also ask

Are English numbers Arabic numbers?

The numbers English speakers use every day, known as Arabic numerals, were developed in the Maghreb during the 10th century. They made their way into Europe through Arab scholars in Al-Andalus (modern-day Andalusia in Spain), hence they are called Arabic numerals.

How can I convert Arabic number to English in SQL?

By using any NLS_PARAMS... select unistr('\0660') from dual; We are getting the Arabic numeral for 0.. With out changing the database character set..

How can I convert Arabic numbers to English in Excel?

Click File > Options > Language. In the Set the Office Language Preferences dialog box, under Choose Display and Help Languages, select the language that you want to use, and then click Set as Default.


4 Answers

Try this:

String number = arabicToDecimal("۴۲"); // number = 42;

private static final String arabic = "\u06f0\u06f1\u06f2\u06f3\u06f4\u06f5\u06f6\u06f7\u06f8\u06f9";
private static String arabicToDecimal(String number) {
    char[] chars = new char[number.length()];
    for(int i=0;i<number.length();i++) {
        char ch = number.charAt(i);
        if (ch >= 0x0660 && ch <= 0x0669)
           ch -= 0x0660 - '0';
        else if (ch >= 0x06f0 && ch <= 0x06F9)
           ch -= 0x06f0 - '0';
        chars[i] = ch;
    }
    return new String(chars);
}
like image 136
Amy Avatar answered Oct 23 '22 01:10

Amy


More generic solution using Character.getNumericValue(char)

static String replaceNonstandardDigits(String input) {
    if (input == null || input.isEmpty()) {
        return input;
    }

    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < input.length(); i++) {
        char ch = input.charAt(i);
        if (isNonstandardDigit(ch)) {
            int numericValue = Character.getNumericValue(ch);
            if (numericValue >= 0) {
                builder.append(numericValue);
            }
        } else {
            builder.append(ch);
        }
    }
    return builder.toString();
}

private static boolean isNonstandardDigit(char ch) {
    return Character.isDigit(ch) && !(ch >= '0' && ch <= '9');
}
like image 39
Flovettee Avatar answered Oct 23 '22 01:10

Flovettee


Kotlin developers:

fun ArabicToEnglish(str: String):String {
        var result = ""
        var en = '0'
        for (ch in str) {
            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
    }
like image 24
Parisa Baastani Avatar answered Oct 23 '22 01:10

Parisa Baastani


A modified generic solution


    fun convertArabic(arabicStr: String): String? {
        val chArr = arabicStr.toCharArray()
        val sb = StringBuilder()
        for (ch in chArr) {
            if (Character.isDigit(ch)) {
                sb.append(Character.getNumericValue(ch))
            }else if (ch == '٫'){
                sb.append(".")
            }

            else {
                sb.append(ch)
            }
        }
        return sb.toString()
    }

The second branch is necessary as double numbers has this character as dot separator '٫'

like image 33
Mahmoud Mabrok Avatar answered Oct 23 '22 00:10

Mahmoud Mabrok