How to Convert Persian Digits in variable to Engli

2020-05-19 19:55发布

I want to change persian numbers which are saved in variable like this :

string Value="۱۰۳۶۷۵۱"; 

to

string Value="1036751";

How can I use easy way like culture info to do this please?

my sample code is:

List<string> NERKHCOlist = new List<string>();
NERKHCOlist = ScrappingFunction(NERKHCO, NERKHCOlist);
int NERKHCO_Price = int.Parse(NERKHCOlist[0]);//NERKHCOlist[0]=۱۰۳۶۷۵۱ 

<= So it can not Parsed it to int
And This is in my function which retun a list with persian digits inside list items

protected List<string> ScrappingFunction(string SiteAddress, List<string> NodesList)
{    
    string Price = "null";
    List<string> Targets = new List<string>();
    foreach (var path in NodesList)
    {
        HtmlNode node = document.DocumentNode.SelectSingleNode(path.ToString());//recognizing Target Node
        Price = node.InnerHtml;//put text of target node in variable(PERSIAN DIGITS)
        Targets.Add(Price);
    }
    return Targets;
}

14条回答
Fickle 薄情
2楼-- · 2020-05-19 20:45

Here my code convert Persian digits in variable to English , By extension method(Can use with dot after your expression)

private static readonly string[] pn = { "۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹" };
    private static readonly string[] en = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
    public static string ToEnglishNumber(this string strNum)
    {
        string chash = strNum;
        for (int i = 0; i < 10; i++)
            chash = chash.Replace(pn[i], en[i]);
        return chash;
    }
    public static string ToEnglishNumber(this int intNum)
    {
        string chash = intNum.ToString();
        for (int i = 0; i < 10; i++)
            chash = chash.Replace(pn[i], en[i]);
        return chash;
    }

and when you want to use this code have to write : txt1.Value.ToEnglishNumber();

查看更多
干净又极端
3楼-- · 2020-05-19 20:48
    public static string ToEnglishNumber(string input)
    {

        var englishnumbers = new Dictionary<string, string>()
        {
            {"۰","0" }, {"۱","1" }, {"۲","2" }, {"۳","3" },{"۴","4" }, {"۵","5" },{"۶","6" }, {"۷","7" },{"۸","8" }, {"۹","9" },
            {"٠","0" }, {"١","1" }, {"٢","2" }, {"٣","3" },{"٤","4" }, {"٥","5" },{"٦","6" }, {"٧","7" },{"٨","8" }, {"٩","9" },

        };

        foreach (var numbers in englishnumbers)
            input = input.Replace(numbers.Key, numbers.Value);

        return input;
    }
查看更多
时光不老,我们不散
4楼-- · 2020-05-19 20:51

You need to parse them first, using e.g. Int32.Parse() with the correct cultural specifier. Once you have it as a plain integer, it's simply a matter of calling ToString() on it, again with the correct cultural specifier.

An alternative solution is to walk the string character by character and just replace any character that is a Persian digit with the corresponding (west) arabic numeral. Other characters can then be preserved as-is, if required.

If the string really contains a number, you should go with the integer parsing method. If it is not just a number, but really a phone number, serial number etc, you might need to use the replacing algorithm instead.

查看更多
太酷不给撩
5楼-- · 2020-05-19 20:52

USE Culture To convert the number from any language to any language

Functions:

public static string ConvertDigitChar(this string str, CultureInfo source, CultureInfo destination)
{
    for (int i = 0; i <= 9; i++)
    {
        str = str.Replace(source.NumberFormat.NativeDigits[i], destination.NumberFormat.NativeDigits[i]);
    }
    return str;
}

public static string ConvertDigitChar(this int digit, CultureInfo destination)
{
    string res = digit.ToString();
    for (int i = 0; i <= 9; i++)
    {
        res = res.Replace(i.ToString(), destination.NumberFormat.NativeDigits[i]);
    }
    return res;
}

How to use the functions:

var fa = CultureInfo.GetCultureInfoByIetfLanguageTag("fa");
var en = CultureInfo.GetCultureInfoByIetfLanguageTag("en");
string str = "۰0۱1۲2۳3۴4۵5۶6۷7۸8۹9";
string r1 = str.ConvertDigitChar(en, fa);
string r2 = str.ConvertDigitChar(fa, en);
int i = 123456789;
string r3 = i.ConvertDigitChar(fa);

Result:

r1: "۰۰۱۱۲۲۳۳۴۴۵۵۶۶۷۷۸۸۹۹"

r2: "00112233445566778899"

r3: "۰۱۲۳۴۵۶۷۸۹"

查看更多
ら.Afraid
6楼-- · 2020-05-19 20:56

You can manually convert them like so

    char[][] numbers = new char[][]
    {
        "0123456789".ToCharArray(),"persian numbers 0-9 here".ToCharArray()
    };
    public void Convert(string problem)
    {
        for (int x = 0; x <= 9; x++)
        {
            problem.Replace(numbers[0][x], numbers[1][x]);
        }
    }

I don't know the persian numbers so you will have to add them into the char array.

查看更多
我只想做你的唯一
7楼-- · 2020-05-19 21:00

I wrote this extension method to convert Arabic and Persian digits in an string to its Latin representation

public static class Extensions
{
    public static string ConvertDigitsToLatin(this string s)
    {
        var sb = new StringBuilder();
        for (int i = 0; i < s.Length; i++)
        {
            switch (s[i])
            {
                    //Persian digits
                case '\u06f0':
                    sb.Append('0');
                    break;
                case '\u06f1':
                    sb.Append('1');
                    break;
                case '\u06f2':
                    sb.Append('2');
                    break;
                case '\u06f3':
                    sb.Append('3');
                    break;
                case '\u06f4':
                    sb.Append('4');
                    break;
                case '\u06f5':
                    sb.Append('5');
                    break;
                case '\u06f6':
                    sb.Append('6');
                    break;
                case '\u06f7':
                    sb.Append('7');
                    break;
                case '\u06f8':
                    sb.Append('8');
                    break;
                case '\u06f9':
                    sb.Append('9');
                    break;

                    //Arabic digits    
                case '\u0660':
                    sb.Append('0');
                    break;
                case '\u0661':
                    sb.Append('1');
                    break;
                case '\u0662':
                    sb.Append('2');
                    break;
                case '\u0663':
                    sb.Append('3');
                    break;
                case '\u0664':
                    sb.Append('4');
                    break;
                case '\u0665':
                    sb.Append('5');
                    break;
                case '\u0666':
                    sb.Append('6');
                    break;
                case '\u0667':
                    sb.Append('7');
                    break;
                case '\u0668':
                    sb.Append('8');
                    break;
                case '\u0669':
                    sb.Append('9');
                    break;
                default:
                    sb.Append(s[i]);
                    break;
            }
        }
        return sb.ToString();
    }
}
查看更多
登录 后发表回答