I want to change persian numbers which are saved in variable like this :
string Value=\"۱۰۳۶۷۵۱\";
to
string Value=\"1036751\"
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;
}
Use this extension ,also for arabic keyboard for example : "۵", "٥" or "۴", "٤"
static char[][] persianChars = new char[][]
{
"0123456789".ToCharArray(),"۰۱۲۳۴۵۶۷۸۹".ToCharArray()
};
static char[][] arabicChars = new char[][]
{
"0123456789".ToCharArray(),"٠١٢٣٤٥٦٧٨٩".ToCharArray()
};
public static string toPrevalentDigits(this string src)
{
if (string.IsNullOrEmpty(src)) return null;
for (int x = 0; x <= 9; x++)
{
src = src.Replace(persianChars[1][x], persianChars[0][x]);
}
for (int x = 0; x <= 9; x++)
{
src = src.Replace(arabicChars[1][x], arabicChars[0][x]);
}
return src;
}
You can use the Windows.Globalization.NumberFormatting.DecimalFormatter class to parse the string. This will parse strings in any of the supported numeral systems (as long as it is internally coherent).