Open megataps opened 3 years ago
HI @megataps, your issue can be solve by using following implementation. The idea is split the number in chunk of 9 digits first.
private String num2String(long num) {
// omit previous code
String str = Long.valueOf(num).toString();
// zero padding in front of string to prepare for splitting
if (str.length() % 9 > 0) {
str = "000000000".substring(str.length() % 9) + str;
}
// Split into chunks of 9 digits each
List<String> groupOfBillion = new ArrayList<>();
for (int i = 0; i < str.length(); i += 9) {
groupOfBillion.add(str.substring(i, i + 9));
}
String words = "";
for (int i = 0; i < groupOfBillion.size(); i++) {
String nonuple = groupOfBillion.get(i);
// Split into chunks of 3 digits each
List<String> groupOfThousand = new ArrayList<>();
for (int j = 0; j < nonuple.length(); j += 3) {
groupOfThousand.add(nonuple.substring(j, j + 3));
}
String nonupleWords = "";
for (int j = 0; j < groupOfThousand.size(); j++) {
String triple = groupOfThousand.get(j);
String tripleWords = readTriple(triple, doShowZeroHundred(groupOfThousand, j)).trim();
if (!tripleWords.isEmpty()) {
nonupleWords += tripleWords + " " + thousandsName.get(groupOfThousand.size() - 1 - j) + " ";
}
}
nonupleWords = nonupleWords.trim();
if (!nonupleWords.isEmpty()) {
words += " " + nonupleWords.trim() + " " + billionsName.get(groupOfBillion.size() - 1 - i);
}
}
return words.trim();
}
private static final List<String> billionsName = Arrays.asList(
"",
"tỷ",
"tỷ tỷ");
testCase.put(101_002_101_000_000_000L,"Một trăm lẻ một triệu tỷ không trăm lẻ hai nghìn tỷ một trăm lẻ một tỷ");
Theo mình thì số trên đọc như vậy chưa chuẩn, có thể sữa như vậy thì đúng hơn: "Một trăm lẻ một triệu, không trăm lẻ hai nghìn, một trăm lẻ một tỷ"