Search code examples
flutterdartnumbersarabic

Convert arabic number to english number & the reverse in Dart


I want to replace Arabic number with English number.

1-2-3-4-5-6-7-8-9 <== ١-٢-٣-٤-٥-٦-٧-٨-٩


Solution

  • 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;
      }