A TypeScript helper for converting numbers to Indonesian words with proper validation and error handling. Inspired by terbilang-js by Naufal Rabbani.
- Converts integers to Indonesian words (Nol to Kuadriliun)
- Supports negative numbers (with "Negatif" prefix)
- Zero returns "Nol" instead of empty string
- Input validation for non-integer values
- Range validation (up to 999 Kuadriliun / 10^18 - 1)
- Handles edge cases (NaN, Infinity)
- Type-safe TypeScript implementation
Copy the terbilang.ts file to your project:
# Or add as a dependency (when published)
npm install terbilang-tsimport { terbilang } from './terbilang'
// Basic numbers
terbilang(0) // "Nol"
terbilang(1) // "Satu"
terbilang(10) // "Sepuluh"
terbilang(100) // "Seratus"
terbilang(1000) // "Seribu"
terbilang(10000) // "Sepuluh Ribu"
// Larger numbers
terbilang(1234567) // "Satu Juta Dua Ratus Tiga Puluh Empat Ribu Lima Ratus Enam Puluh Tujuh"
terbilang(1000000000) // "Satu Milyar"
// Negative numbers
terbilang(-5) // "Negatif Lima"
terbilang(-100) // "Negatif Seratus"Converts a number to its Indonesian word representation.
Parameters:
num- The number to convert. Must be an integer.
Returns:
- Indonesian word representation of the number
Throws:
Error- If the input is not an integerError- If the number exceeds the supported range (≥ 10^18)
Edge Cases:
terbilang(NaN)→""terbilang(Infinity)→""terbilang(0)→"Nol"
The function supports integers from -999999999999999999 to 999999999999999999 (999 Kuadriliun).
Numbers outside this range will throw an error:
terbilang(10 ** 18) // throws Error// Non-integer numbers
try {
terbilang(123.45)
} catch (e) {
console.error(e.message) // "terbilang only accepts integer numbers"
}
// Numbers beyond supported range
try {
terbilang(10 ** 18)
} catch (e) {
console.error(e.message) // "terbilang only supports numbers up to 999 Kuadriliun"
}npm testMIT