How to Convert English Digits to Arabic Digits

How to convert an English number to an Arabic number e.g., 196 to ١٩٦?

There's no such method, but we can implement it; let's put the task as general as we can:

Given a string source and CultureInfo culture, turn all digits
within source into national digits if culture provides them

Code:

  using System.Globalization;
using System.Linq;

...

public static partial class StringExtensions {
public static String ConvertNumerals(this string source,
CultureInfo culture = null) {
if (null == source)
return null;

if (null == culture)
culture = CultureInfo.CurrentCulture;

string[] digits = culture.NumberFormat.NativeDigits.Length >= 10
? culture.NumberFormat.NativeDigits
: CultureInfo.InvariantCulture.NumberFormat.NativeDigits;

return string.Concat(source
.Select(c => char.IsDigit(c)
? digits[(int) (char.GetNumericValue(c) + 0.5)]
: c.ToString()));
}
}

Demo:

  // "ar-SA" is "arabic Saudi Arabia"
Console.WriteLine("test 123".ConvertNumerals(CultureInfo.GetCultureInfo("ar-SA")));
// "en-US" is "english United States"
Console.WriteLine("test 123".ConvertNumerals(CultureInfo.GetCultureInfo("en-US")));

Outcome:

test ١٢٣
test 123

How can i convert English digits to Arabic digits?

Thy this workaround (just list all cultures you want to use this numerals in the string array):

private static class ArabicNumeralHelper
{
public static string ConvertNumerals(this string input)
{
if (new string[] { "ar-lb", "ar-SA" }
.Contains(Thread.CurrentThread.CurrentCulture.Name))
{
return input.Replace('0', '\u06f0')
.Replace('1', '\u06f1')
.Replace('2', '\u06f2')
.Replace('3', '\u06f3')
.Replace('4', '\u06f4')
.Replace('5', '\u06f5')
.Replace('6', '\u06f6')
.Replace('7', '\u06f7')
.Replace('8', '\u06f8')
.Replace('9', '\u06f9');
}
else return input;
}
}

Then use the method, for all of your strings you want to have 'central Arabic numerals' in, like this:

DateTime.Now.ToString().ConvertNumerals();

Convert English numbers to Arabic numerals

If you are referring to what Wikipedia calls eastern arabic / indic numerals, a simple replace operation should do.

$western_arabic = array('0','1','2','3','4','5','6','7','8','9');
$eastern_arabic = array('٠','١','٢','٣','٤','٥','٦','٧','٨','٩');

$str = str_replace($western_arabic, $eastern_arabic, $str);

How to convert english "abc123 " digits to arabic "ابث١٢٣ " dynamically android

You can do it with replaceAll.

First, create a method:

    public String convertToArabic(int value)
{
String newValue = (((((((((((value+"")
.replaceAll("1", "١")).replaceAll("2", "٢"))
.replaceAll("3", "٣")).replaceAll("4", "٤"))
.replaceAll("5", "٥")).replaceAll("6", "٦"))
.replaceAll("7", "٧")).replaceAll("8", "٨"))
.replaceAll("9", "٩")).replaceAll("0", "٠"));
return newValue;
}

And usage:

String myArabicNumber= convertToArabic(123);
Log.d("output",myArabicNumber);

output:

١٢٣ 

UPDATE:

you can do it by concatenating them:

    String space = "\u00A0"; //space
String myArabicCharacter = getResources().getString(R.string.help);
String myArabicNumber= convertToArabic(123);
String fullHomework = myArabicCharacter+space+myArabicNumber;
Log.d("output",fullHomework);

output:

     راهنما ١٢٣

Update2

YourProject/
res/
values/
strings.xml
values-ar/
strings.xml

arabic string resource:
<string name="help">راهنما</string>

english string resource:
<string name="help">help</string>

how to convert Arabic numbers to English numbers in swift?

You need to convert the arabic number string to english first and then do the calculation part.

    let numberStr: String = "٨٦٩١٢٨٨١"
let formatter: NumberFormatter = NumberFormatter()
formatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let final = formatter.number(from: numberStr)
let doubleNumber = Double(final!)
print("\(doubleNumber)")

Convert arabic number to english number & the reverse in Dart

Arabic to English

String replaceArabicNumber(String input) {
const english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
const arabic = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];

for (int i = 0; i < english.length; i++) {
input = input.replaceAll(arabic[i], english[i]);
}
print("$input");
return input;
}

to do the opposite replacing the English numbers with the Arabic ones(English to Arabic)

 String replaceEnglishNumber(String input) {
const english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
const arabic = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];

for (int i = 0; i < english.length; i++) {
input = input.replaceAll(english[i], arabic[i]);
}
print("$input");
return input;
}

convert english number with farsi or arabic number in Dart

Example:

String replaceFarsiNumber(String input) {
const english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
const farsi = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];

for (int i = 0; i < english.length; i++) {
input = input.replaceAll(english[i], farsi[i]);
}

return input;
}

main() {
print(replaceFarsiNumber('0-1-2-3-4-5-6-7-8-9')); // ==> ۰-۱-۲-۳-۴-۵-۶-۷-۸-۹
}


Related Topics



Leave a reply



Submit