Skip to content

Instantly share code, notes, and snippets.

@antronic
Forked from nantcom/getbathtext.js
Created March 5, 2017 10:21
Show Gist options
  • Save antronic/8bca8c344f50a87f3ba296f897f7e7c3 to your computer and use it in GitHub Desktop.
Save antronic/8bca8c344f50a87f3ba296f897f7e7c3 to your computer and use it in GitHub Desktop.
Get Thai Baht Text using JavaScript
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) + "สตางค์";
}
};
@SurasakNong
Copy link

เพิ่มเงื่อนไขนี้เข้าไปใน for ด้วยจะดีมาก
if (i == 0 && number == 1) {
output = "เอ็ด";
continue;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment