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 ==> ۱-۲-۳-۴-۵-۶-۷-۸-۹
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')); // ==> ۰-۱-۲-۳-۴-۵-۶-۷-۸-۹
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With