我需要转换阿拉伯/波斯数字到它的英语相等(例如,转换"۲“到"2")
我该怎么做呢?
发布于 2011-03-16 02:17:03
我建议你有一个十位的查找字符串,并一次替换掉所有的数字。
public static void main(String... args) {
System.out.println(arabicToDecimal("۴۲"));
}
//used in Persian apps
private static final String extendedArabic = "\u06f0\u06f1\u06f2\u06f3\u06f4\u06f5\u06f6\u06f7\u06f8\u06f9";
//used in Arabic apps
private static final String arabic = "\u0660\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u0668\u0669";
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);
}
打印
42
使用字符串作为查找的原因是,其他字符(如.
、-
、,
)将保留原样。事实上,十进制数是不变的。
发布于 2015-02-10 18:18:19
我发现了一种更简单、更快的方法,它也包含了两个阿拉伯代码页。
public static String convertToEnglishDigits(String value)
{
String newValue = value.replace("١", "1").replace("٢", "2").replace("٣", "3").replace("٤", "4").replace("٥", "5")
.replace("٦", "6").replace("7", "٧").replace("٨", "8").replace("٩", "9").replace("٠", "0")
.replace("۱", "1").replace("۲", "2").replace("۳", "3").replace("۴", "4").replace("۵", "5")
.replace("۶", "6").replace("۷", "7").replace("۸", "8").replace("۹", "9").replace("۰", "0");
return newValue;
}
如果你改变替换方式,它将以英文格式返回数字,反之亦然。
(“·”,"0")到("0",“·”)
发布于 2012-09-25 00:08:03
试试这些家伙:
/**
* Utility class to detect arabic languages and convert numbers into arabic digits.
*
* @author Ahmed Shakil
* @date 09-24-2012
*/
public final class ArabicUtil {
private static final char[] DIGITS = {'\u0660','\u0661','\u0662','\u0663','\u0664','\u0665','\u0666','\u0667','\u0668','\u0669'};
/**
* Returns <code>true</code> if the provided language code uses arabic characters; othersise <code>false</code>.
* @param lang ISO language code.
* @return <code>true</code> if the provided language code uses arabic characters; othersise <code>false</code>
*/
public static boolean isArabic (String lang) {
return "ar".equals(lang) || "fa".equals(lang) || "ur".equals(lang);
}
/**
* Convert digits in the specified string to arabic digits.
*/
public static String convertDigits (String str) {
if (str == null || str.length() == 0) return str;
char[] s = new char[str.length()];
for(int i =0;i<s.length;i++)
s[i] = toDigit( str.charAt( i ) );
return new String(s);
}
/**
* Convert single digit in the specified string to arabic digit.
*/
public static char toDigit (char ch) {
int n = Character.getNumericValue( (int)ch );
return n >=0 && n < 10 ? ARABIC[n] : ch;
}
/**
* Convert an int into arabic string.
*/
public static String toString (int num) {
return convertDigits( Integer.toString( num ) );
}
}
顺便说一句,阿拉伯数字和乌尔都语/波斯语之间有一个区别:阿拉伯语:
private static final char[] ARABIC = {'\u0660', '\u0661', '\u0662', '\u0663', '\u0664', '\u0665', '\u0666', '\u0667', '\u0668', '\u0669'};
乌尔都语或波斯语:
private static final char[] URDU_FARSI = {'\u06f0', '\u06f1', '\u06f2', '\u06f3', '\u06f4', '\u06f5', '\u06f6', '\u06f7', '\u06f8', '\u06f9'};
https://stackoverflow.com/questions/5316131
复制相似问题