将string转换为java中的另一个语言环境

你好
我需要将阿拉伯语/波斯语数字转换成与英语相等(例如,将“2”转换为“2”)
我该怎么做?

谢谢

我build议你有一个十位数的查找string,并一次replace所有的数字。

public static void main(String... args) { System.out.println(arabicToDecimal("۴۲")); } 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); } 

版画

 42 

使用string作为查找的原因是其他字符,如. -将保持原样。 实际上十进制数字不会改变。

我通过java.math.BigDecimal类来实现这一点,下面是代码片段

 String arabicNumerals = "۴۲۴۲.۴۲"; String englishNumerals = new BigDecimal(arabic).toString(); System.out.println("Number In Arabic : "+arabicNumerals); System.out.println("Number In English : "+englishNumerals); 

结果

 Number In Arabic : ۴۲۴۲.۴۲ Number In English : 4242.42 

注意:如果在阿拉伯数字中有数字以外的任何字符,上述代码将不起作用,例如:4,242.42将导致java.lang.NumberFormatException ,所以您可以使用Character.isDigit(char ch)在另一个逻辑和使用上面的代码。 所有正常情况下都很有魅力!

美好的一天

试试这个家伙:

 /** * 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'}; 

我发现了一个更简单,更快捷的方法,其中包括两个阿拉伯语代码页。

 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; } 

它将返回英文格式的数字,反之亦然,如果您更改从replace。
(“0”,“0”)到(“0”,“0”)

首先使它工作,然后使它看起来不错;-)

 public static char persianDigitToEnglish(char persianDigit) { return (char) (((int)persianDigit) - ((int)'۲' - (int)'2')); } 

作品2 ,不幸的是我不知道其他波斯数字,你可以试试吗?

 assertThat(persianDigitToEnglish('۲')).isEqualTo('2'); 

编辑 :(基于Peter Lawreystring版本,但使用StringBuilder

 public static String persianDigitToEnglish(String persianNumber) { StringBuilder chars = new StringBuilder(persianNumber.length()); for (int i = 0; i < persianNumber.length(); i++) chars.append(persianDigitToEnglish(persianNumber.charAt(i))); return chars.toString(); } private static char persianDigitToEnglish(char persianDigit) { return (char) (((int)persianDigit) - ((int)'۲' - (int)'2')); } 

它也会使用小数点..

 public class mainsupport { public static void main(String args[]){ // String Numtoconvert="15.3201" ; // String Numtoconvert="458" ; String Numtoconvert="٨٧٫٥٩٨" ; // integer value 87.598 System.out.println(getUSNumber(Numtoconvert)); } private static String getUSNumber(String Numtoconvert){ NumberFormat formatter = NumberFormat.getInstance(Locale.US); try { if(Numtoconvert.contains("٫")) Numtoconvert=formatter.parse(Numtoconvert.split("٫")[0].trim())+"."+formatter.parse(Numtoconvert.split("٫")[1].trim()); else Numtoconvert=formatter.parse(Numtoconvert).toString(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return Numtoconvert; } 

打印87.598

这么简单的答案:

 public static String convertNumbersToPersian(String str) { String answer = str; answer = answer.replace("1","١"); answer = answer.replace("2","٢"); answer = answer.replace("3","٣"); answer = answer.replace("4","٤"); answer = answer.replace("5","٥"); answer = answer.replace("6","٦"); answer = answer.replace("7","٧"); answer = answer.replace("8","٨"); answer = answer.replace("9","٩"); answer = answer.replace("0","٠"); return answer; } 

 public static String convertNumbersToEnglish(String str) { String answer = str; answer = answer.replace("١", "1"); answer = answer.replace("٢", "2"); answer = answer.replace("٣", "3"); answer = answer.replace("٤", "4"); answer = answer.replace("٥", "5"); answer = answer.replace("٦", "6"); answer = answer.replace("٧", "7"); answer = answer.replace("٨", "8"); answer = answer.replace("٩", "9"); answer = answer.replace("٠", "0"); return answer; } 

使用Locale类来转换数字。

  Locale locale = new Locale("ar"); String formattedArabic = format(locale, "%d", value)); 

我认为最好的办法是将Locale更改为你想要的,例如,
双号码:

 NumberFormat fmt = NumberFormat.getNumberInstance(Locale.US); d = Double.parseDouble(s); 

对于string:

 NumberFormat.getNumberInstance(Locale.US).format(s); 

或DecimalFormat:

 double num; DecimalFormat df = new DecimalFormat("###.###"); df.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US)); String s = df.format(num); 

我认为,而不是逐个replace数字(这将只适用于十进制数字),你应该parsing你的号码波斯的NumberFormat为一个数字,然后(如有必要)使用英文NumberFormat再次格式化。