numberToWords converts numeric values into Persian word form (123 → "صد و بیست و سه").
Function
Input rules
- Accepts
number | string. Strings are first run throughremoveCommas. - The numeric value must be a safe integer (
Number.isSafeInteger). - Non-safe-integer input (floats, values above
Number.MAX_SAFE_INTEGER) returns aPersianToolsTypeErrorinstance (the function returns the error; it does not throw). - Negative integers work — output is prefixed with
منفی.
Return type — union with the error
string | PersianToolsTypeError. Don’t assume it’s a plain string.
Range
- Min:
-Number.MAX_SAFE_INTEGER - Max:
Number.MAX_SAFE_INTEGER(≈ 9.007e15) - Floats: rejected.
Ordinal mode
{ ordinal: true } runs the result through addOrdinalSuffix. Suffix rules:
- Ends with
ی→ appendاُم(with leading space). - Ends with
سه→ strip last 2 chars, appendسوم. - Otherwise → append
م.
Pitfalls
- No
currencyorscaleoptions. Older docs reference them — they do not exist. - No locale switching. Output is always Persian words separated by
و. - Return is
string | PersianToolsTypeError— guard accordingly. - Persian-digit string input is not auto-normalized —
removeCommasonly handles commas. RunautoConvertDigitsToENupstream if needed.
Composition
Source
src/modules/numberToWords/index.ts, constants.ts, types.ts · Tests: test/NumberToWords.spec.ts