Skip to content

Instantly share code, notes, and snippets.

@luanvuhlu
Last active March 27, 2024 08:54
Show Gist options
  • Save luanvuhlu/5d395c6e94321abd77ed to your computer and use it in GitHub Desktop.
Save luanvuhlu/5d395c6e94321abd77ed to your computer and use it in GitHub Desktop.
Read money from number to Vietnam Dong text by Java and Javascript
function readGroup(group) {
let readDigit = [" Không", " Một", " Hai", " Ba", " Bốn", " Năm", " Sáu", " Bảy", " Tám", " Chín"];
var temp = "";
if (group == "000") return "";
temp = readDigit[parseInt(group.substring(0, 1))] + " Trăm";
if (group.substring(1, 2) == "0")
if (group.substring(2, 3) == "0") return temp;
else {
temp += " Lẻ" + readDigit[parseInt(group.substring(2, 3))];
return temp;
}
else
temp += readDigit[parseInt(group.substring(1, 2))] + " Mươi";
if (group.substring(2, 3) == "5") temp += " Lăm";
else if (group.substring(2, 3) != "0") temp += readDigit[parseInt(group.substring(2, 3))];
return temp;
}
function readMoney(num) {
if ((num == null) || (num == "")) return "";
let temp = "";
while (num.length < 18) {
num = "0" + num;
}
let g1 = num.substring(0, 3);
let g2 = num.substring(3, 6);
let g3 = num.substring(6, 9);
let g4 = num.substring(9, 12);
let g5 = num.substring(12, 15);
let g6 = num.substring(15, 18);
if (g1 != "000") {
temp = readGroup(g1);
temp += " Triệu";
}
if (g2 != "000") {
temp += readGroup(g2);
temp += " Nghìn";
}
if (g3 != "000") {
temp += readGroup(g3);
temp += " Tỷ";
} else if ("" != temp) {
temp += " Tỷ";
}
if (g4 != "000") {
temp += readGroup(g4);
temp += " Triệu";
}
if (g5 != "000") {
temp += readGroup(g5);
temp += " Nghìn";
}
temp = temp + readGroup(g6);
temp = temp.replaceAll("Một Mươi", "Mười");
temp = temp.trim();
temp = temp.replaceAll("Không Trăm", "");
temp = temp.trim();
temp = temp.replaceAll("Mười Không", "Mười");
temp = temp.trim();
temp = temp.replaceAll("Mươi Không", "Mươi");
temp = temp.trim();
if (temp.indexOf("Lẻ") == 0) temp = temp.substring(2);
temp = temp.trim();
temp = temp.replaceAll("Mươi Một", "Mươi Mốt");
temp = temp.trim();
let result = temp.substring(0, 1).toUpperCase() + temp.substring(1).toLowerCase();
return (result == "" ? "Không" : result) + " đồng chẵn"
}
private String readGroup(String group){
String[] readDigit = {" Không", " Một", " Hai", " Ba", " Bốn", " Năm", " Sáu", " Bảy", " Tám", " Chín" };
String temp = "";
if (group == "000") return "";
//read number hundreds
temp = readDigit[Integer.parseInt(group.substring(0,1))] + " Trăm";
//read number tens
if (group.substring(1,2).equals("0"))
if (group.substring(2,3).equals("0")) return temp;
else
{
temp += " Lẻ" + readDigit[Integer.parseInt(group.substring(2,3))];
return temp;
}
else
temp += readDigit[Integer.parseInt(group.substring(1,2))] + " Mươi";
//read number
if (group.substring(2,3) == "5") temp += " Lăm";
else if (group.substring(2,3) != "0") temp += readDigit[Integer.parseInt(group.substring(2,3))];
return temp;
}
public String readMoney(String num){
if((num==null)||(num.equals(""))) return "";
String temp = "";
//length <= 18
while (num.length() < 18)
{
num = "0" + num;
}
String g1 = num.substring(0, 3);
String g2 = num.substring(3, 6);
String g3 = num.substring(6, 9);
String g4 = num.substring(9, 12);
String g5 = num.substring(12,15);
String g6 = num.substring(15,18);
//read group1 ---------------------
if (!g1.equals("000")){
temp = readGroup(g1);
temp += " Triệu";
}
//read group2-----------------------
if (!g2.equals("000")){
temp += readGroup(g2);
temp += " Nghìn";
}
//read group3 ---------------------
if (!g3.equals("000")){
temp += readGroup(g3);
temp += " Tỷ";
} else if(!"".equals(temp)){
temp += " Tỷ";
}
//read group2-----------------------
if (!g4.equals("000")){
temp += readGroup(g4);
temp += " Triệu";
}
//---------------------------------
if (!g5.equals("000")){
temp += readGroup(g5);
temp += " Nghìn";
}
//-----------------------------------
temp = temp + readGroup(g6);
//---------------------------------
// Refine
temp = temp.replaceAll("Một Mươi", "Mười");
temp = temp.trim();
temp = temp.replaceAll("Không Trăm", "");
// if (temp.indexOf("Không Trăm") == 0) temp = temp.substring(10);
temp = temp.trim();
temp = temp.replaceAll("Mười Không", "Mười");
temp = temp.trim();
temp = temp.replaceAll("Mươi Không", "Mươi");
temp = temp.trim();
if (temp.indexOf("Lẻ")==0) temp = temp.substring(2);
temp = temp.trim();
temp = temp.replaceAll("Mươi Một", "Mươi Mốt");
temp = temp.trim();
//Change Case
return "("+temp.substring(0, 1).toUpperCase() + temp.substring(1).toLowerCase() + " đồng chẵn./.)";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment