npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

thb-text

v1.0.2

Published

Thai Baht Text converter - converts numbers to Thai text representation for currency amounts

Readme

Thai Baht Text

A TypeScript library for converting numbers to Thai text representation for currency amounts, supporting Thai Baht and other monetary units.

Features

  • แปลงตัวเลขเป็นข้อความภาษาไทย (เช่น 121.50 → "หนึ่งร้อยยี่สิบเอ็ดบาทห้าสิบสตางค์")
  • รองรับหน่วยต่างๆ (บาท ล้านบาท พันล้านบาท ล้านล้านบาท)
  • การใช้ "เอ็ด"
  • รองรับจำนวนติดลบ
  • กำหนดทศนิยมได้สำหรับหน่วยที่ไม่ใช่บาท
  • เพิ่มคำว่า "ถ้วน" ได้

Installation

npm install thb-text

Usage

การใช้งานพื้นฐาน

import { thaiBahtText, ThaiBahtTextUtil } from 'thb-text';

// ใช้ฟังก์ชัน utility
console.log(thaiBahtText(121.50)); // "หนึ่งร้อยยี่สิบเอ็ดบาทห้าสิบสตางค์"

// ใช้ static method ของ class
console.log(ThaiBahtTextUtil.thaiBahtText(121.50)); // "หนึ่งร้อยยี่สิบเอ็ดบาทห้าสิบสตางค์"

Number Prototype Extension (ไม่บังคับ)

หากต้องการใช้ method กับ Number objects โดยตรง:

import { registerNumberPrototype } from 'thb-text';

// ลงทะเบียน prototype extension
registerNumberPrototype();

// ตอนนี้สามารถใช้ method กับตัวเลขโดยตรงได้
console.log((121.50).thaiBahtText()); // "หนึ่งร้อยยี่สิบเอ็ดบาทห้าสิบสตางค์"

การใช้งานแบบปรับแต่งพารามิเตอร์

import { ThaiBahtTextUtil, Unit } from 'thb-text';

// ใช้ options object
const options = {
  unit: Unit.Million,
  decimalPlaces: 2,
  appendBahtOnly: false
};

console.log(ThaiBahtTextUtil.thaiBahtText(12.5, options)); // "สิบสองจุดห้าล้านบาท"

// ใช้ parameters แยก
console.log(ThaiBahtTextUtil.thaiBahtText(
  101, 
  Unit.Baht, 
  2, 
  true
)); // "หนึ่งร้อยหนึ่งบาทถ้วน"

การใช้จำนวนทศนิยม (decimalPlaces)

สำหรับหน่วยบาท

  • ใช้ทศนิยม 2 ตำแหน่งเสมอ (แสดงเป็นสตางค์)
  • พารามิเตอร์ decimalPlaces ไม่มีผลกับหน่วยบาท
// หน่วยบาท - ทศนิยมคือสตางค์
thaiBahtText(12.34); // "สิบสองบาทสามสิบสี่สตางค์"
thaiBahtText(12.34, Unit.Baht, 5); // "สิบสองบาทสามสิบสี่สตางค์" (ยังคง 2 ทศนิยม)

สำหรับหน่วยอื่น (ล้าน/พันล้าน/ล้านล้าน)

  • กำหนดจำนวนทศนิยมได้ตามต้องการ (1-6 ตำแหน่ง)
  • แสดงเป็น "จุด" ตามด้วยตัวเลขภาษาไทย
// ล้านบาท - ทศนิยมต่างๆ
thaiBahtText(12345600, Unit.Million, 1, false); // "สิบสองจุดสามล้านบาท"
thaiBahtText(12345600, Unit.Million, 2, false); // "สิบสองจุดสามห้าล้านบาท" 
thaiBahtText(12345600, Unit.Million, 4, false); // "สิบสองจุดสามสี่ห้าหกล้านบาท"

// พันล้านบาท
thaiBahtText(5500000000, Unit.Billion, 1, false); // "ห้าจุดห้าพันล้านบาท"
thaiBahtText(5500000000, Unit.Billion, 2, false); // "ห้าจุดห้าศูนย์พันล้านบาท"

// ล้านล้านบาท
thaiBahtText(3250000000000, Unit.Trillion, 3, false); // "สามจุดสองห้าศูนย์ล้านล้านบาท"

เมื่อไหร่ควรใช้ทศนิยม

  • หน่วยล้าน: เมื่อต้องการแสดงจำนวนเงินให้ละเอียดมากขึ้น
  • หน่วยพันล้าน: สำหรับตัวเลขขนาดใหญ่ที่ต้องการความแม่นยำ
  • หน่วยล้านล้าน: สำหรับตัวเลขมหาศาล เช่น งบประมาณประเทศ
// ตัวอย่างการใช้งานจริง
const budget = 2500000000; // งบ 2.5 พันล้าน

// แสดงแบบไม่มีทศนิยม
thaiBahtText(budget, Unit.Billion, 0, false); // "สองพันล้านบาท"

// แสดงแบบมีทศนิยม 1 ตำแหน่ง  
thaiBahtText(budget, Unit.Billion, 1, false); // "สองจุดห้าพันล้านบาท"

// แสดงแบบมีทศนิยม 2 ตำแหน่ง
thaiBahtText(budget, Unit.Billion, 2, false); // "สองจุดห้าศูนย์พันล้านบาท"

API Reference

Enums

Unit (หน่วย)

  • Baht (0): บาท
  • Million (1): ล้านบาท
  • Billion (2): พันล้านบาท
  • Trillion (3): ล้านล้านบาท

Functions

thaiBahtText(amount, options?)

thaiBahtText(amount, unit?, decimalPlaces?, appendBahtOnly?)

แปลงตัวเลขเป็นข้อความภาษาไทย

Parameters:

  • amount: number | null | undefined - จำนวนเงินที่ต้องการแปลง
  • options: Partial<ThaiBahtTextOptions> - ตัวเลือกการตั้งค่า (ไม่บังคับ)
  • unit: Unit - หน่วยของเงิน (ค่าเริ่มต้น: Unit.Baht)
  • decimalPlaces: number - จำนวนทศนิยมสำหรับหน่วยที่ไม่ใช่บาท (ค่าเริ่มต้น: 2)
  • appendBahtOnly: boolean - เพิ่มคำว่า "ถ้วน" หรือไม่ (ค่าเริ่มต้น: true)

Returns: string - ข้อความภาษาไทย

ThaiBahtTextUtil.thaiBahtText(...)

Static method ที่มี signature เหมือนกับฟังก์ชันหลัก

registerNumberPrototype()

ลงทะเบียน Number prototype extension เพื่อใช้ method กับตัวเลขโดยตรง

Number.prototype.thaiBahtText(...)

Extension method สำหรับ Number objects (ต้องเรียก registerNumberPrototype() ก่อน)

Constants

  • ThaiBahtTextUtil.MAX_VALUE: ค่าสูงสุดที่รองรับ (999,999,999,999.99)
  • ThaiBahtTextUtil.MIN_VALUE: ค่าต่ำสุดที่รองรับ (-999,999,999,999.99)

ตัวอย่างการใช้งาน

// ตัวเลขพื้นฐาน
console.log(thaiBahtText(1)); // "หนึ่งบาทถ้วน"
console.log(thaiBahtText(25)); // "ยี่สิบห้าบาทถ้วน"

// จำนวนทศนิยม
console.log(thaiBahtText(12.34)); // "สิบสองบาทสามสิบสี่สตางค์"

// จำนวนติดลบ
console.log(thaiBahtText(-50.25)); // "ลบห้าสิบบาทยี่สิบห้าสตางค์"

// จำนวนมาก
console.log(thaiBahtText(1234567)); // "หนึ่งล้านสองแสนสามหมื่นสี่พันห้าร้อยหกสิบเจ็ดบาทถ้วน"

// หน่วยต่างๆ
console.log(thaiBahtText(12.5, Unit.Million)); // "สิบสองจุดห้าล้านบาทถ้วน"

// ตัวเลขที่มี 1 ที่หลักหน่วย (การใช้ "เอ็ด" ที่ถูกต้อง)
console.log(thaiBahtText(101)); // "หนึ่งร้อยหนึ่งบาทถ้วน" (ใช้ "หนึ่ง" ไม่ใช่ "เอ็ด")
console.log(thaiBahtText(21)); // "ยี่สิบเอ็ดบาทถ้วน" (ใช้ "เอ็ด" เฉพาะหลักสิบ)

Testing

npm test

For test coverage:

npm run test:coverage

Building

npm run build

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.