Last active
July 15, 2020 10:37
-
-
Save TheBrown/3d4aab2ce9174005241d7db260b8328d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const convertToLaoNumberString = (num, suffix) => { | |
"use strict"; | |
if (typeof suffix === "undefined") { | |
suffix = "ຖ້ວນ"; | |
} | |
num = num || 0; | |
num = num.toString().replace(/[, ]/g, ""); // remove commas, spaces | |
if (isNaN(num) || Math.round(parseFloat(num) * 100) / 100 === 0) { | |
return "ສູນຖ້ວນ"; | |
} else { | |
var t = ["", "ສິບ", "ຮ້ອຍ", "ພັນ", "ໝື່ນ", "ແສນ", "ລ້ານ"], | |
n = [ | |
"", | |
"ໜຶ່ງ", | |
"ສອງ", | |
"ສາມ", | |
"ສີ່", | |
"ຫ້າ", | |
"ຫົກ", | |
"ເຈັດ", | |
"ແປດ", | |
"ເກົ້າ", | |
], | |
len, | |
digit, | |
text = "", | |
parts, | |
i; | |
if (num.indexOf(".") > -1) { | |
// have decimal | |
/* | |
* precision-hack | |
* more accurate than parseFloat the whole number | |
*/ | |
parts = num.toString().split("."); | |
num = parts[0]; | |
const decimal = parts[1]; | |
parts[1] = parseFloat("0." + parts[1]); | |
parts[1] = (Math.round(parts[1] * 100) / 100).toString(); // more accurate than toFixed(2) | |
parts = parts[1].split("."); | |
if (parts.length > 1 && parts[1].length === 1) { | |
parts[1] = parts[1].toString() + "0"; | |
} | |
num = parseInt(num, 10) + parseInt(parts[0], 10); | |
/* | |
* end - precision-hack | |
*/ | |
text = num ? convertToLaoNumberString(num) : ""; | |
const translateNumber = (number) => { | |
switch (number) { | |
case "0": | |
return "ສູນ"; | |
case "1": | |
return "ໜຶ່ງ"; | |
case "2": | |
return "ສອງ"; | |
case "3": | |
return "ສາມ"; | |
case "4": | |
return "ສີ່"; | |
case "5": | |
return "ຫ້າ"; | |
case "6": | |
return "ຫົກ"; | |
case "7": | |
return "ເຈັດ"; | |
case "8": | |
return "ແປດ"; | |
case "9": | |
return "ເກົ້າ"; | |
default: | |
return number; | |
} | |
}; | |
const convertToRegularLaoNumber = (number) => { | |
const numberArray = number.split(""); | |
console.log(numberArray); | |
const laoNumbers = numberArray.map((item) => { | |
return translateNumber(item); | |
}); | |
return laoNumbers.join(""); | |
}; | |
if (parts[1]) { | |
text = text + "ຈຸດ" + convertToRegularLaoNumber(decimal); | |
} | |
if (parseInt(parts[1], 10) > 0) { | |
text = text.replace("ຖ້ວນ", ""); | |
} | |
return text; | |
} else { | |
console.log({ num }); | |
if (num.length > 7) { | |
// more than (or equal to) 10 millions | |
var overflow = num.substring(0, num.length - 6); | |
var remains = num.slice(-6); | |
return ( | |
convertToLaoNumberString(overflow).replace("ຖ້ວນ", "ລ້ານ") + | |
convertToLaoNumberString(remains).replace("ສູນ", "") | |
); | |
} else { | |
len = num.length; | |
for (i = 0; i < len; i = i + 1) { | |
digit = parseInt(num.charAt(i), 10); | |
if (digit > 0) { | |
if ( | |
len > 2 && | |
i === len - 1 && | |
digit === 1 && | |
suffix !== "ຈຸດ" | |
) { | |
text += "ເອັດ" + t[len - 1 - i]; | |
} else { | |
text += n[digit] + t[len - 1 - i]; | |
} | |
} | |
} | |
// grammar correction | |
text = text.replace("ໜຶ່ງສິບ", "ສິບ"); | |
text = text.replace("ສອງສິບ", "ຊາວ"); | |
text = text.replace("ສິບໜຶ່ງ", "ສິບເອັດ"); | |
console.log({ text, suffix }); | |
return text + suffix; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment