英文:
Change a numeric value as a string type in Persian to Latin
问题
// 以下是要翻译的内容:
我想要改变数字字符串的本地化(将所有字符从源本地化更改为目标本地化,例如:从 -fa 到 en- 或从 -mm 到 en-,反之亦然),例如,如果我有以下内容:
```java
String value = "꧱꧲꧳";
这在缅甸语中是 123,或者:
String value = "۱۲۳";
这在波斯语中相同。
想要更改为 123,但要使用一个将本地化作为参数的函数,而不是逐个迭代字符。
下面的代码将数字在 'en' 中更改为任何其他本地化,但它有一些问题,它以 double 作为参数,但我需要传递一个字符串:
NumberFormat fmt = NumberFormat.getNumberInstance(new Locale("fa"));
fmt.setMaximumFractionDigits(100);
String result = fmt.format(123);
<details>
<summary>英文:</summary>
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 :
```java
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);
答案1
得分: 3
这是内置的。
Locale myanmar = Locale.forLanguageTag("my-MM");
NumberFormat format = NumberFormat.getIntegerInstance(myanmar);
String value = "꧱꧲꧳";
Number result = format.parse(value);
System.out.println(result);
输出结果为:
> 123
根据您的需求进行定制。
将数字转换为字符串称为格式化。相反的转换是解析。执行这些转换所需调用的方法分别以相应的方式命名。
英文:
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.
答案2
得分: 0
这是你想要将英文格式的数字转换为波斯语的对吧?这个类提供了许多有用的方法。
tv1.setText(Persian.persianalize(myText);
tv2.setText(Persian.persianalize(myNumber);
而这个类:
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);
}
}
英文:
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);
}
}
答案3
得分: 0
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
}
// Use this way
"꧱꧲꧳".toJustEnglishNumber()
英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论