-
-
Save antronic/8bca8c344f50a87f3ba296f897f7e7c3 to your computer and use it in GitHub Desktop.
Get Thai Baht Text using JavaScript
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
var getBathText = function (inputNumber) { | |
var getText = function (input) { | |
var toNumber = input.toString(); | |
var numbers = toNumber.split('').reverse(); | |
var numberText = "/หนึ่ง/สอง/สาม/สี่/ห้า/หก/เจ็ด/แปด/เก้า/สิบ".split('/'); | |
var unitText = "/สิบ/ร้อย/พ้น/หมื่น/แสน/ล้าน".split('/'); | |
var output = ""; | |
for (var i = 0; i < numbers.length; i++) { | |
var number = parseInt(numbers[i]); | |
var text = numberText[number]; | |
var unit = unitText[i]; | |
if (number == 0) | |
continue; | |
if (i == 1 && number == 2) { | |
output = "ยี่สิบ" + output; | |
continue; | |
} | |
if (i == 1 && number == 1) { | |
output = "สิบ" + output; | |
continue; | |
} | |
output = text + unit + output; | |
} | |
return output; | |
} | |
var fullNumber = Math.floor(inputNumber); | |
var decimal = inputNumber - fullNumber; | |
if (decimal == 0) { | |
return getText(fullNumber) + "บาทถ้วน"; | |
} else { | |
// convert decimal into full number, need only 2 digits | |
decimal = decimal * 100; | |
decimal = Math.round(decimal); | |
return getText(fullNumber) + "บาท" + getText(decimal) + "สตางค์"; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
เพิ่มเงื่อนไขนี้เข้าไปใน for ด้วยจะดีมาก
if (i == 0 && number == 1) {
output = "เอ็ด";
continue;
}