UNPKG

word-number-word

Version:

A TypeScript/JavaScript utility for converting numbers to words and vice versa — including support for decimals, negatives, and currency converting.

24 lines (23 loc) 1.07 kB
import { numberToWord } from "./numberToWord"; const CURRENCY_LABELS = { usd: { major: "dollar", minor: "cent" }, bdt: { major: "taka", minor: "poisha" }, eur: { major: "euro", minor: "cent" }, gbp: { major: "pound", minor: "pence" }, }; export function currencyToWord(amount, currency) { if (!(currency in CURRENCY_LABELS)) { throw new Error(`Unsupported currency type: "${currency}". Supported types: ${Object.keys(CURRENCY_LABELS).join(", ")}`); } const { major, minor } = CURRENCY_LABELS[currency]; const isNegative = amount < 0; const absAmount = Math.abs(amount); const majorValue = Math.floor(absAmount); const minorValue = Math.round((absAmount - majorValue) * 100); const majorWord = numberToWord(majorValue); const minorWord = numberToWord(minorValue); const majorUnit = majorValue === 1 ? major : `${major}s`; const minorUnit = minorValue === 1 ? minor : `${minor}s`; const result = `${isNegative ? "minus " : ""}${majorWord} ${majorUnit} and ${minorWord} ${minorUnit}`; return result; }