| Crates.io | thai-number-text |
| lib.rs | thai-number-text |
| version | 1.0.0 |
| created_at | 2025-12-30 17:42:03.912365+00 |
| updated_at | 2025-12-30 17:42:03.912365+00 |
| description | Convert numbers to Thai text with Baht currency, accounting style, and scientific notation support |
| homepage | |
| repository | https://github.com/ttwrpz/thai-number-text |
| max_upload_size | |
| id | 2013080 |
| size | 80,699 |
A minimal, no_std-compatible Rust crate for converting numbers to Thai textual representations.
Zero dependencies by default.
#![no_std] compatible (uses alloc only)serde support[dependencies]
thai-number-text = "1.0.0"
| Feature | Description |
|---|---|
std |
Enable std (for benchmarks) |
serde |
Enable Serialize/Deserialize |
use thai_number_text::{to_text_i64, to_text_f64, to_baht, ThaiTextOptions};
let opts = ThaiTextOptions::new();
// Integers
assert_eq!(to_text_i64(21, &opts), "ยี่สิบเอ็ด");
assert_eq!(to_text_i64(101, &opts), "หนึ่งร้อยหนึ่ง"); // Standard: no เอ็ด
// Floats
assert_eq!(to_text_f64(0.123, &opts), "ศูนย์จุดหนึ่งสองสาม");
// Currency
assert_eq!(to_baht(100.50, &opts), "หนึ่งร้อยบาทห้าสิบสตางค์");
use thai_number_text::{to_text_i64, EtMode, ThaiTextOptions};
// Colloquial (default): เอ็ด only when tens digit exists
// This is the common everyday usage in Thailand
let opts = ThaiTextOptions::new();
assert_eq!(to_text_i64(11, &opts), "สิบเอ็ด"); // has tens digit
assert_eq!(to_text_i64(101, &opts), "หนึ่งร้อยหนึ่ง"); // no tens digit
assert_eq!(to_text_i64(111, &opts), "หนึ่งร้อยสิบเอ็ด"); // has tens digit
// RoyalInstitute (ราชบัณฑิตยสภา): Always use เอ็ด for trailing 1
// This is the formal/official Thai language standard
let opts = ThaiTextOptions::royal_institute();
assert_eq!(to_text_i64(101, &opts), "หนึ่งร้อยเอ็ด");
assert_eq!(to_text_i64(1001, &opts), "หนึ่งพันเอ็ด");
// Military: Never use เอ็ด
// Used in Thai military communications for clarity
let opts = ThaiTextOptions::military();
assert_eq!(to_text_i64(11, &opts), "สิบหนึ่ง");
assert_eq!(to_text_i64(21, &opts), "ยี่สิบหนึ่ง");
use thai_number_text::{to_text_i64, ThaiTextOptions};
let opts = ThaiTextOptions::new();
// Million (ล้าน)
assert_eq!(to_text_i64(1_000_000, &opts), "หนึ่งล้าน");
// Trillion (ล้านล้าน)
assert_eq!(to_text_i64(1_000_000_000_000, &opts), "หนึ่งล้านล้าน");
// Quintillion (ล้านล้านล้าน) - handles up to i64::MAX
let result = to_text_i64(i64::MAX, &opts);
// "เก้าล้านล้านล้าน..."
use thai_number_text::{to_baht, ThaiTextOptions};
let opts = ThaiTextOptions::accounting();
assert_eq!(to_baht(100.50, &opts), "หนึ่งร้อยบาทห้าสิบสตางค์ถ้วน");
use thai_number_text::{to_text_str, ThaiTextOptions};
let opts = ThaiTextOptions::new();
// Basic string input
assert_eq!(to_text_str("21", &opts).unwrap(), "ยี่สิบเอ็ด");
assert_eq!(to_text_str("0.5", &opts).unwrap(), "ศูนย์จุดห้า");
// Scientific notation strings
assert_eq!(to_text_str("1e6", &opts).unwrap(), "หนึ่งล้าน");
assert_eq!(to_text_str("1.5e10", &opts).unwrap(), "หนึ่งหมื่นห้าพันล้าน");
// Beyond i64::MAX (10^24)
let result = to_text_str("1000000000000000000000000", &opts).unwrap();
// "หนึ่งล้านล้านล้านล้าน"
use thai_number_text::{to_text_scientific, ThaiTextOptions};
let opts = ThaiTextOptions::new();
// 9.22 × 10^37
assert_eq!(
to_text_scientific(9.22, 37, &opts),
"เก้าจุดสองสอง คูณ สิบยกกำลังสามสิบเจ็ด"
);
// Negative exponent: 5.5 × 10^-3
assert_eq!(
to_text_scientific(5.5, -3, &opts),
"ห้าจุดห้า คูณ สิบยกกำลังลบสาม"
);
| Rule | Description | Example |
|---|---|---|
| เอ็ด (Colloquial) | Use only when tens digit exists | 101 → หนึ่งร้อยหนึ่ง |
| เอ็ด (RoyalInstitute) | Use for any trailing 1 (formal) | 101 → หนึ่งร้อยเอ็ด |
| ยี่ (yi) | Used for 2 in tens position | 20 → ยี่สิบ |
| สิบ | Omits หนึ่ง prefix for 10-19 | 10 → สิบ (not หนึ่งสิบ) |
| ล้านล้าน | Trillion (10^12) | 1,000,000,000,000 |
| ล้านล้านล้าน | Quintillion (10^18) | i64::MAX region |
// Direct functions (smallest code size)
pub fn to_text_i64(n: i64, opts: &ThaiTextOptions) -> String;
pub fn to_text_f64(n: f64, opts: &ThaiTextOptions) -> String;
pub fn to_baht(amount: f64, opts: &ThaiTextOptions) -> String;
// Generic wrapper
pub fn to_text<N: Into<ThaiNumber>>(n: N, opts: &ThaiTextOptions) -> String;
// String input (arbitrarily large numbers)
pub fn to_text_str(s: &str, opts: &ThaiTextOptions) -> Result<String, ParseError>;
// Scientific notation
pub fn to_text_scientific(mantissa: f64, exponent: i64, opts: &ThaiTextOptions) -> String;
// Options
pub enum EtMode { Colloquial, RoyalInstitute, Military }
pub struct ThaiTextOptions { et_mode: EtMode, accounting_style: bool }
cargo run --example demo --features std
This project is MIT licensed (see LICENSE), maintaining the same license as the original work.