将波斯语中的数值字符串更改为拉丁字符。

huangapple go评论70阅读模式
英文:

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 = &quot;꧱꧲꧳&quot;;

that is 123 in MYANMAR language, or :

String value = &quot;۱۲۳&quot;;

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(&quot;fa&quot;));
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(&quot;my-MM&quot;);
	NumberFormat format = NumberFormat.getIntegerInstance(myanmar);
	
	String value = &quot;꧱꧲꧳&quot;;
	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[]{ &quot;۰&quot;, &quot;۱&quot;, &quot;۲&quot;, &quot;۳&quot;, &quot;۴&quot;, &quot;۵&quot;, &quot;۶&quot;, &quot;۷&quot;, &quot;۸&quot;, &quot;۹&quot; };
public static String persianalize(String text, boolean zeroToText) {
if (text==null)
return &quot;&quot;;
if (text.length() == 0) {
return &quot;&quot;;
}
String out = &quot;&quot;;
int length = text.length();
for (int i = 0; i &lt; length; i++) {
char c = text.charAt(i);
if (&#39;0&#39; &lt;= c &amp;&amp; c &lt;= &#39;9&#39;) {
int number = Integer.parseInt(String.valueOf(c));
out += persianNumbers[number];
} else if (c == &#39;٫&#39;) {
out += &#39;،&#39;;
} else {
out += c;
}
}
if (zeroToText)
if (out!=null &amp;&amp; out.equals(&quot;۰&quot;))
return &quot;صفر&quot;;
return out;
}
public static String convertNumberToEnglish(String num) {
if (num==null)
return &quot;&quot;;
String d = num;
d = d.replace(&quot;۰&quot;, &quot;0&quot;);
d = d.replace(&quot;۱&quot;, &quot;1&quot;);
d = d.replace(&quot;۲&quot;, &quot;2&quot;);
d = d.replace(&quot;٣&quot;, &quot;3&quot;);
d = d.replace(&quot;٤&quot;, &quot;4&quot;);
d = d.replace(&quot;۵&quot;, &quot;5&quot;);
d = d.replace(&quot;٦&quot;, &quot;6&quot;);
d = d.replace(&quot;٧&quot;, &quot;7&quot;);
d = d.replace(&quot;۸&quot;, &quot;8&quot;);
d = d.replace(&quot;۹&quot;, &quot;9&quot;);
d = d.replace(&quot;،&quot;, &quot;,&quot;);
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 = &quot;&quot;
for (character in this.toCharArray()) {
var str = &quot;&quot;
when (val ascii = character.toInt()) {
in 1632..1641 -&gt; {
// Arabic
val valueOld = ascii - 1584
val valueChar = valueOld.toChar()
str = valueChar.toString()
}
in 1776..1785 -&gt; {
// Persian
val valueOld = ascii - 1728
val valueChar = valueOld.toChar()
str = valueChar.toString()
}
in 48..57 -&gt; {
// English
str = character.toString()
}
}
value += str
}
return value
}

and use this way

&quot;꧱꧲꧳&quot;.toJustEnglishNumber()

huangapple
  • 本文由 发表于 2020年9月7日 12:40:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63771471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定