Class ThaiBaht

java.lang.Object
io.github.zazalng.ThaiBaht

public final class ThaiBaht extends Object
Primary entry point for converting monetary amounts (BigDecimal) into beautifully formatted Thai Baht text in multiple languages.

This immutable class provides both static convenience methods and instance-based APIs to convert numeric currency amounts to their textual representation. It supports multiple output languages (Thai and English) and flexible formatting options through the ThaiBahtConfig configuration object.

Core Functionality

  • Multi-language support: Convert to Thai (บาท/สตางค์) or English (Baht/Satang)
  • Precise handling: Amounts are normalized to 2 decimal places (satang precision)
  • Negative support: Customizable prefixes for negative amounts
  • Flexible formatting: Control unit word inclusion and custom output formats
  • Thread-safe: All instances are immutable and safe for concurrent use

Usage Examples

Static API (simplest approach)


 // Thai (default)
 String thai = ThaiBaht.of(new BigDecimal("1234.56"));
 // Output: "หนึ่งพันสองร้อยสามสิบสี่บาทห้าสิบหกสตางค์"

 // English
 String english = ThaiBaht.of(
     new BigDecimal("1234.56"),
     ThaiBahtConfig.builder(Language.ENGLISH).build()
 );
 // Output: "One Thousand Two Hundred Thirty-Four Baht Fifty-Six Satang"
 

Instance API (for reuse and chaining)


 ThaiBaht converter = ThaiBaht.create(new BigDecimal("500.25"));
 String result = converter.setAmount(new BigDecimal("1000.50")).toString();
 // Output: "หนึ่งพันบาทห้าสิบสตางค์"
 

Fluent Configuration


 ThaiBaht converter = ThaiBaht.create(new BigDecimal("100"))
     .config(builder -> builder
         .language(Language.ENGLISH)
         .useUnit(true)
         .formal(true)
     );
 String result = converter.toString();
 // Output: "One Hundred Baht Only"
 

Key Features

  • Accurate Thai Grammar: Handles special Thai number formatting rules including "เอ็ด" (1 in units of larger numbers), "ยี่" (2 in tens), and repeating "ล้าน" (millions)
  • Satang Precision: Correctly displays "ถ้วน" (Only) when no satang are present
  • Large Number Support: Handles amounts in the billions and beyond
  • Custom Formatting: Apply custom format strings with named placeholders for flexible output
  • Negative Amount Handling: Configure custom prefixes for negative values

Design Notes

This class is immutable and designed for both convenience (static methods) and flexibility (builder pattern). All monetary computations use BigDecimal to avoid floating-point precision issues. The class is optimized for enterprise applications including payment processors, e-invoicing, and government form generation.
Since:
1.0
Version:
1.4.0
Author:
Zazalng
See Also:
  • Method Details

    • create

      public static ThaiBaht create(BigDecimal amount)
      Creates a new ThaiBaht converter instance with the specified amount and default configuration.

      The default configuration includes Thai language output, unit words enabled ("บาท", "สตางค์"), and formal wording rules. This is the simplest way to create a converter for standard use cases.

      Example:

      
       ThaiBaht converter = ThaiBaht.create(new BigDecimal("1234.56"));
       System.out.println(converter);  // Output: "หนึ่งพันสองร้อยสามสิบสี่บาทห้าสิบหกสตางค์"
       
      Parameters:
      amount - the monetary amount to convert, must not be null
      Returns:
      a new ThaiBaht instance configured with default settings
      Throws:
      NullPointerException - if amount is null
      See Also:
    • create

      public static ThaiBaht create(BigDecimal amount, ThaiBahtConfig config)
      Creates a new ThaiBaht converter instance with the specified amount and custom configuration.

      This method allows you to control all aspects of the text conversion, including the output language, inclusion of unit words, custom format templates, and negative number prefixes. The instance is immutable but supports fluent method chaining through setter methods.

      Example with English output:

      
       ThaiBahtConfig config = ThaiBahtConfig.builder(Language.ENGLISH)
           .useUnit(true)
           .build();
       ThaiBaht converter = ThaiBaht.create(new BigDecimal("500.50"), config);
       System.out.println(converter);  // Output: "Five Hundred Baht Fifty Satang"
       

      Example with custom format:

      
       ThaiBahtConfig config = ThaiBahtConfig.builder()
           .setFormatTemplate("{INTEGER}{UNIT}{FLOAT?และ{FLOAT}{SATANG}}")
           .build();
       ThaiBaht converter = ThaiBaht.create(new BigDecimal("100.50"), config);
       System.out.println(converter);  // Custom formatting with placeholders
       
      Parameters:
      amount - the monetary amount to convert, must not be null
      config - the formatting configuration controlling language, units, format, and more, must not be null
      Returns:
      a new ThaiBaht instance with the specified configuration
      Throws:
      NullPointerException - if either amount or config is null
      See Also:
    • setAmount

      public ThaiBaht setAmount(BigDecimal amount)
      Updates the monetary amount to be converted.

      This method allows changing the amount while keeping the same configuration. It returns this instance to support fluent method chaining for convenient workflow patterns.

      Example:

      
       ThaiBaht converter = ThaiBaht.create(new BigDecimal("100"))
           .setAmount(new BigDecimal("500"))
           .setAmount(new BigDecimal("1000.50"));
       System.out.println(converter);  // Final amount: 1000.50
       
      Parameters:
      amount - the new monetary amount to convert, must not be null
      Returns:
      this ThaiBaht instance for method chaining
      Throws:
      NullPointerException - if amount is null
    • getAmount

      public BigDecimal getAmount()
      Returns the current monetary amount held by this instance.

      The amount represents the complete currency value in baht, including fractional satang.

      Returns:
      the current BigDecimal amount, never null
    • getConfig

      public ThaiBahtConfig getConfig()
      Returns the current formatting configuration held by this instance.

      The configuration controls language, unit words, custom formats, and negative number handling.

      Returns:
      the current ThaiBahtConfig, never null
    • setConfig

      public ThaiBaht setConfig(ThaiBahtConfig config)
      Updates the formatting configuration used by this instance.

      This method allows changing the language, unit words, format templates, and other configuration options while keeping the same amount. It returns this instance to support fluent method chaining.

      Parameters:
      config - the new formatting configuration, must not be null
      Returns:
      this ThaiBaht instance for method chaining
      Throws:
      NullPointerException - if config is null
    • config

      public ThaiBaht config(Consumer<ThaiBahtConfig.Builder> updater)
      Modifies the current configuration using a consumer function for flexible updates.

      This method provides a convenient fluent interface for modifying configuration without creating a new builder from scratch. The configuration builder is created from the current config, passed to the updater function for modification, then built back into this instance.

      Example - switching languages:

      
       ThaiBaht converter = ThaiBaht.create(new BigDecimal("100"))
           .config(b -> b.language(Language.ENGLISH).useUnit(false));
       System.out.println(converter);  // English output without unit words
       

      Example - adding custom format:

      
       converter.config(b -> b.setFormatTemplate("{INTEGER}{UNIT} ({FLOAT} {SATANG})"));
       
      Parameters:
      updater - a consumer function that accepts a ThaiBahtConfig.Builder for modification, must not be null
      Returns:
      this ThaiBaht instance for method chaining
      Throws:
      NullPointerException - if updater is null
      See Also:
    • toString

      public String toString()
      Converts the stored amount to Thai baht text using the current configuration.

      This method performs the actual currency-to-text conversion, routing to the appropriate language handler (Thai or English) based on the configuration language setting. The output respects all configuration options including unit words, custom formats, and negative prefixes.

      Overrides:
      toString in class Object
      Returns:
      the formatted textual representation of the amount according to the configuration, never null
    • of

      public static String of(BigDecimal amount)
      Converts the given amount to Thai baht text using the default configuration.

      This is a convenience static method that creates a one-time converter using the default configuration. The default includes:

      • Output language: Thai (with Thai digits and unit words)
      • Unit words: Enabled (includes "บาท" and "สตางค์")
      • Formal wording: Enabled
      • Negative prefix: "ลบ" (Thai minus)

      The amount is normalized to 2 decimal places (satang precision). Negative values are prefixed with the language's default negative prefix.

      Examples:

      
       // Positive amount
       String result = ThaiBaht.of(new BigDecimal("1234.56"));
       // Output: "หนึ่งพันสองร้อยสามสิบสี่บาทห้าสิบหกสตางค์"
      
       // Zero amount
       String zero = ThaiBaht.of(new BigDecimal("0"));
       // Output: "ศูนย์บาทถ้วน"
      
       // Negative amount
       String negative = ThaiBaht.of(new BigDecimal("-100.50"));
       // Output: "ลบหนึ่งร้อยบาทห้าสิบสตางค์"
       
      Parameters:
      amount - the monetary amount to convert (baht and satang), must not be null
      Returns:
      the Thai-language textual representation of the amount, never null
      Throws:
      NullPointerException - if amount is null
      See Also:
    • of

      public static String of(BigDecimal amount, ThaiBahtConfig config)
      Converts the given amount to Thai baht text using the provided configuration.

      This method offers fine-grained control over output formatting by allowing a custom ThaiBahtConfig to be specified. Use this for one-time conversions that require non-default formatting options. You can customize:

      Usage Examples:

      Example 1 - English output with unit words:

      
       String english = ThaiBaht.of(
           new BigDecimal("500.25"),
           ThaiBahtConfig.builder(Language.ENGLISH)
               .useUnit(true)
               .build()
       );
       // Output: "Five Hundred Baht Twenty-Five Satang"
       

      Example 2 - Thai without unit words:

      
       String noUnits = ThaiBaht.of(
           new BigDecimal("100.50"),
           ThaiBahtConfig.builder()
               .useUnit(false)
               .build()
       );
       // Output: "หนึ่งร้อยห้าสิบ"
       

      Example 3 - Custom format template:

      
       String custom = ThaiBaht.of(
           new BigDecimal("100.50"),
           ThaiBahtConfig.builder()
               .setFormatTemplate("[{INTEGER} {UNIT}] + [{FLOAT} {SATANG}]")
               .build()
       );
       // Output: "[หนึ่งร้อย บาท] + [ห้าสิบ สตางค์]"
       

      Example 4 - Custom negative prefix:

      
       String negative = ThaiBaht.of(
           new BigDecimal("-50.00"),
           ThaiBahtConfig.builder()
               .setPrefix("ติดลบ")
               .build()
       );
       // Output: "ติดลบห้าสิบบาทถ้วน"
       
      Parameters:
      amount - the monetary amount to convert (baht and satang), must not be null
      config - formatting configuration to control output language, unit words, and formatting options, must not be null
      Returns:
      the textual representation of the amount according to the specified configuration, never null
      Throws:
      NullPointerException - if either amount or config is null
      See Also: