toVietnameseWords method
Implementation
String toVietnameseWords() {
if (this == 0) {
return 'Không';
}
if (this < 0) {
return 'Âm ${(-this).toVietnameseWords().toLowerCase()}';
}
String s = toString();
List<String> groups =
(VietnameseNumberWords.zeroLeftPadding[s.length % 3] + s)
.chunks(3)
.toList();
bool showZeroHundred = VietnameseNumberWords.shouldShowZeroHundred(groups);
int index = -1;
String rawResult = groups.fold('', (String acc, String e) {
index++;
String triple = VietnameseNumberWords.readTriple(
e,
showZeroHundred && index > 0,
);
String multipleThousand = triple.trim().isEmpty
? ''
: VietnameseNumberWords.multipleThousand[groups.length - 1 - index];
return '$acc $triple $multipleThousand';
});
return RegExp(r'\s+').allMatches(rawResult).isEmpty
? rawResult.trim().capitalize()
: rawResult.replaceAll(RegExp(r'\s+'), ' ').trim().capitalize();
}