Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert english number with farsi or arabic number in Dart

Tags:

flutter

dart

I'm new in Dart and flutter.

I want to replace English number with Farsi number. How can implement this?

1-2-3-4-5-6-7-8-9 ==> ‍‍۱-۲-۳-۴-۵-۶-۷-۸-۹
like image 584
Saman Avatar asked Nov 20 '18 13:11

Saman


2 Answers

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'));  // ==>  ۰-۱-۲-۳-۴-۵-۶-۷-۸-۹
}
like image 124
Xavier Avatar answered Sep 22 '22 22:09

Xavier


Not sure in what context you're going to use the numbers, but I would rather define a const map: const numberMap = {0: '۰', 1: '۱', 2:'۲', 3:'۳', 4:'٤', 5:'۵', 6:'٦', 7:'۷', 8:'۸',9: '۹'}; Then you can just call numberMap[number] to reuse it.

like image 34
stt106 Avatar answered Sep 23 '22 22:09

stt106