I have been trying to convert the number into my own currency number style in Flutter. For example, I have a number of these.
99,99,99,99,99,999
I need to have them converted into the words like
Ninety nine Kharba Ninety nine Arba Ninety nine crore Ninety nine Lakhs Ninety nine thousand Nine hundred and Ninety nine.
I tried using the number_to_character package but I got the words in English Currency format.
I tried as follows:
var converter = NumberToCharacterConverter('en');
converter.convertInt(int.parse(amountController.text));
words=words[0] words.substring(1);
I got nine trillion nine hundred and ninety nine billion nine hundred and ninety nine million nine hundred and ninety nine thousand nine hundred and ninety nine
Also, I tried with Locale NumberFormat
from intl but I got INR10LCr
for the same amount I mentioned above it also did not have the solution I expected.
How can I convert the number into the words mentioned above in Flutter?
CodePudding user response:
This is a stripped-down version of to-words
from Javascript world.
String convert(int number) {
final isNegativeNumber = number < 0;
if (isNegativeNumber) {
number = number.abs();
}
final words = <String>[];
final match =
numberWordsMapping.entries.firstWhere((entry) => number >= entry.key);
if (number < 100) {
words.add(match.value);
number -= match.key;
if (number > 0) {
words.add(convert(number));
}
} else {
final quotient = number ~/ match.key;
final remainder = number % match.key;
words
..add(convert(quotient))
..add(match.value);
if (remainder > 0) {
words.add(convert(remainder));
}
}
if (isNegativeNumber) words.insert(0, 'Minus');
return words.join(' ');
}
const numberWordsMapping = {
100000000000: 'Kharba',
1000000000: 'Arba',
10000000: 'Crore',
100000: 'Lakh',
1000: 'Thousand',
100: 'Hundred',
90: 'Ninety',
80: 'Eighty',
70: 'Seventy',
60: 'Sixty',
50: 'Fifty',
40: 'Forty',
30: 'Thirty',
20: 'Twenty',
19: 'Nineteen',
18: 'Eighteen',
17: 'Seventeen',
16: 'Sixteen',
15: 'Fifteen',
14: 'Fourteen',
13: 'Thirteen',
12: 'Twelve',
11: 'Eleven',
10: 'Ten',
9: 'Nine',
8: 'Eight',
7: 'Seven',
6: 'Six',
5: 'Five',
4: 'Four',
3: 'Three',
2: 'Two',
1: 'One',
0: 'Zero',
};
OUTPUT
1 = One
10 = Ten
12 = Twelve
30 = Thirty
42 = Forty Two
99 = Ninety Nine
111 = One Hundred Eleven
101 = One Hundred One
100 = One Hundred
999 = Nine Hundred Ninety Nine
1000 = One Thousand
1001 = One Thousand One
9990 = Nine Thousand Nine Hundred Ninety Nine
10000 = Ten Thousand
10042 = Ten Thousand Forty Two
9999999999999 = Ninety Nine Kharba Ninety Nine Arba Ninety Nine Crore Ninety Nine Lakh Ninety Nine Thousand Nine Hundred Ninety Nine