Class TextConverter
This package-private utility class serves as the central conversion dispatcher. It is not
intended for direct use by library consumers; instead, use the public ThaiBaht API.
This class routes conversion requests to the appropriate language-specific handler based on
the configured Language.
Conversion Process
The conversion process follows these steps:
- Receives a
ThaiBahtinstance containing an amount and configuration - Validates that the amount and configuration are present
- Routes to the appropriate language handler:
THAI→ThaiConvertHandler.convert(ThaiBaht)ENGLISH→EnglishConvertHandler.convert(ThaiBaht)
- Returns the formatted text from the language handler
Amount Normalization
Each language-specific handler internally normalizes the amount to 2 decimal places using
RoundingMode.DOWN (truncation). This ensures consistency in satang
precision across all conversions.
Language-Specific Rules
Each language handler implements specialized conversion logic:
- Thai: Applies Thai grammar rules for special cases like "เอ็ด", "ยี่", and repeating "ล้าน"
- English: Applies English conventions including hyphens for compound numbers and standard position words (Hundred, Thousand, Million)
Configuration Handling
The converter respects all configuration options from ThaiBahtConfig, including:
- Unit word inclusion/exclusion
- Custom format templates
- Negative amount prefixes
- Formal wording rules
Thread Safety
This class is stateless and thread-safe. All instance state comes from the input
ThaiBaht object, and the conversion algorithm maintains no mutable state.
- Since:
- 1.0
- Version:
- 1.3.0
- Author:
- Zazalng
- See Also:
- Implementation Note:
- This class is package-private and should not be accessed from outside this package.
Use the
ThaiBahtpublic API instead.
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringtoBahtText(ThaiBaht baht) Converts aThaiBahtinstance to language-specific baht text.
-
Method Details
-
toBahtText
Converts aThaiBahtinstance to language-specific baht text.This method is the core internal conversion entry point for the library. It takes a
ThaiBahtinstance containing an amount and configuration, validates the inputs, and routes the conversion to the language handler in the configuration. The result respects all configuration options.Conversion Process
The conversion process follows these steps:
- Receives a
ThaiBahtinstance containing an amount and configuration - Validates that the amount and configuration are present
- Routes to the language handler stored in the configuration via
ThaiBahtConfig.getLanguageHandler() - Returns the formatted text from the language handler
Amount Normalization
The amount is normalized to 2 decimal places (satang precision) using
RoundingMode.DOWN. This ensures that amounts like 100.567 are truncated to 100.56 (56 satang), not rounded.Negative Handling
Negative amounts are handled by the language handler by extracting the absolute value and prepending the configured negative prefix (or handler's default if not specified).
Example conversions:
// Thai positive ThaiBaht baht1 = ThaiBaht.create(new BigDecimal("1234.56")); String thai = TextConverter.toBahtText(baht1); // Result: "หนึ่งพันสองร้อยสามสิบสี่บาทห้าสิบหกสตางค์" // English with units ThaiBahtConfig engConfig = ThaiBahtConfig.builder(new EnglishLanguageHandler()) .useUnit(true).build(); ThaiBaht baht2 = ThaiBaht.create(new BigDecimal("500.50"), engConfig); String english = TextConverter.toBahtText(baht2); // Result: "Five Hundred Baht Fifty Satang" // Negative amount ThaiBaht baht3 = ThaiBaht.create(new BigDecimal("-100")); String negative = TextConverter.toBahtText(baht3); // Result: "ลบหนึ่งร้อยบาทถ้วน"- Parameters:
baht- the ThaiBaht instance containing amount and configuration, must not benull- Returns:
- the language-specific textual representation of the amount, never
null - Throws:
IllegalArgumentException- if the amount isnull- Since:
- 2.0.0
- See Also:
- Receives a
-