convertToArabicWords function
Converts an integer into Arabic words. Supports numbers from 0 to 9999.
Example:
logFile(key: convertToArabicWords(1234), name: 'convert to arabic words'); // "ألف ومائتان وأربعة وثلاثون"
logFile(key: convertToArabicWords(15), name: 'convert to arabic words'); // "خمسة عشر"
logFile(key: convertToArabicWords(1000), name: 'convert to arabic words'); // "ألف"
Implementation
String convertToArabicWords(int number) {
if (number == 0) return "صفر";
final List<String> ones = [
"",
"واحد",
"اثنان",
"ثلاثة",
"أربعة",
"خمسة",
"ستة",
"سبعة",
"ثمانية",
"تسعة",
];
final List<String> teens = [
"عشرة",
"أحد عشر",
"اثنا عشر",
"ثلاثة عشر",
"أربعة عشر",
"خمسة عشر",
"ستة عشر",
"سبعة عشر",
"ثمانية عشر",
"تسعة عشر",
];
final List<String> tens = [
"",
"عشرة",
"عشرون",
"ثلاثون",
"أربعون",
"خمسون",
"ستون",
"سبعون",
"ثمانون",
"تسعون",
];
final List<String> hundreds = [
"",
"مائة",
"مائتان",
"ثلاثمائة",
"أربعمائة",
"خمسمائة",
"ستمائة",
"سبعمائة",
"ثمانمائة",
"تسعمائة",
];
String convertThreeDigits(int num) {
String result = "";
int h = num ~/ 100;
int t = (num % 100) ~/ 10;
int o = num % 10;
if (h > 0) {
result += hundreds[h];
if (t > 0 || o > 0) result += " و";
}
if (t > 1) {
result += tens[t];
if (o > 0) result += " و${ones[o]}";
} else if (t == 1) {
result += teens[o];
} else {
result += ones[o];
}
return result;
}
String result = "";
int thousands = number ~/ 1000;
int remainder = number % 1000;
if (thousands > 0) {
if (thousands == 1) {
result += "ألف";
} else if (thousands == 2) {
result += "ألفان";
} else if (thousands >= 3 && thousands <= 10) {
result += "${convertThreeDigits(thousands)} آلاف";
} else {
result += "${convertThreeDigits(thousands)} ألف";
}
if (remainder > 0) result += " و";
}
if (remainder > 0) {
result += convertThreeDigits(remainder);
}
return result;
}