Hassan Yousefzadeh
Hassan Yousefzadeh

Reputation: 110

Change a numeric value as a string type in Persian to Latin

I want to change the local of the numeric string (change all character from source local to destination local eg: -fa to en- or -mm to en- and vice versa ) for example if I have something like :

String value = "꧱꧲꧳";

that is 123 in MYANMAR language, or :

String value = "۱۲۳";

that is the same but in the Persian language.

want to change to 123 but with the use of a function that gives the local as a parameter, not by iterate character by character.

The following code changes the number in 'en' to any other local but it has some problem that it gives a double as a parameter but I need to pass a string:

NumberFormat fmt = NumberFormat.getNumberInstance(new Locale("fa"));
fmt.setMaximumFractionDigits(100);
String result = fmt.format(123);

Upvotes: 1

Views: 280

Answers (3)

Rasoul Miri
Rasoul Miri

Reputation: 12222

you can use this extension in kotlin

fun String.toJustEnglishNumber(): String {

    var value = ""

    for (character in this.toCharArray()) {

        var str = ""

        when (val ascii = character.toInt()) {

            in 1632..1641 -> {
                // Arabic
                val valueOld = ascii - 1584
                val valueChar = valueOld.toChar()
                str = valueChar.toString()
            }
            in 1776..1785 -> {
                // Persian
                val valueOld = ascii - 1728
                val valueChar = valueOld.toChar()
                str = valueChar.toString()
            }
            in 48..57 -> {
                // English
                str = character.toString()
            }
        }
        value += str
    }
    return value
}

and use this way

"꧱꧲꧳".toJustEnglishNumber()

Upvotes: 0

Anonymous
Anonymous

Reputation: 86379

That’s built in.

    Locale myanmar = Locale.forLanguageTag("my-MM");
    NumberFormat format = NumberFormat.getIntegerInstance(myanmar);
    
    String value = "꧱꧲꧳";
    Number result = format.parse(value);
    System.out.println(result);

Output is:

123

Tailor to your needs.

Converting a number to a string is called formatting. The opposite conversion is parsing. And the methods you need to call to perform those conversions are named accordingly.

Upvotes: 3

sajjad
sajjad

Reputation: 847

So you are trying to convert English formatted numbers to Persian, yeah? This class gives you a bunch of useful methods.

tv1.setText(Persian.persianalize(myText);
tv2.setText(Persian.persianalize(myNumber);

And the class:

public class Persian {

private static String[] persianNumbers = new String[]{ "۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹" };

public static String persianalize(String text, boolean zeroToText) {
    if (text==null)
        return "";

    if (text.length() == 0) {
        return "";
    }
    String out = "";
    int length = text.length();
    for (int i = 0; i < length; i++) {
        char c = text.charAt(i);
        if ('0' <= c && c <= '9') {
            int number = Integer.parseInt(String.valueOf(c));
            out += persianNumbers[number];
        } else if (c == '٫') {
            out += '،';
        } else {
            out += c;
        }
    }
    if (zeroToText)
        if (out!=null && out.equals("۰"))
            return "صفر";
    return out;
}

public static String convertNumberToEnglish(String num) {
    if (num==null)
        return "";
    String d = num;
    d = d.replace("۰", "0");
    d = d.replace("۱", "1");
    d = d.replace("۲", "2");
    d = d.replace("٣", "3");
    d = d.replace("٤", "4");
    d = d.replace("۵", "5");
    d = d.replace("٦", "6");
    d = d.replace("٧", "7");
    d = d.replace("۸", "8");
    d = d.replace("۹", "9");
    d = d.replace("،", ",");

    return d;
}

public static String persianalize(long value, boolean zeroToText) {
    return persianalize(String.valueOf(value), zeroToText);
}

public static String persianalize(long value) {
    return persianalize(String.valueOf(value), false);
}

public static String persianalize(double value) {
    return persianalize(String.valueOf(value), false);
}

public static String persianalize(String value) {
    return persianalize(value, false);
}

}

Upvotes: 0

Related Questions