bnspellnum

JavaScript / TypeScript

bnspellnum on npm. ESM, CommonJS and a browser build, with TypeScript types. Zero dependencies.

Install

Node 18 or newer, or any modern browser. npmjs.com/package/bnspellnum

npm install bnspellnum

Quick start

import { words, currency, ordinal, year, date, toBnDigits, toEnDigits } from "bnspellnum";
// or: const { words } = require("bnspellnum");

words(1250000);                          // বারো লাখ পঞ্চাশ হাজার
words(200, { style: "formal" });         // দুইশত
words("-3.25");                          // ঋণাত্মক তিন দশমিক দুই পাঁচ
words(-7, { negativeStyle: "colloquial" }); // মাইনাস সাত
words(10n ** 21n);                       // এক কোটি কোটি কোটি
currency("1250.50", { cheque: true });   // এক হাজার দুইশো পঞ্চাশ টাকা পঞ্চাশ পয়সা মাত্র
ordinal(21, { short: true });            // ২১তম
year(1971);                              // উনিশশো একাত্তর
date(16, 12, { year: 1971 });            // ষোলোই ডিসেম্বর উনিশশো একাত্তর
date(28, 11, { style: "short" });        // ২৮শে নভেম্বর
toEnDigits("১২,৫০,০০০");                 // 12,50,000

API

SignatureReturns
words(n, { style?, negativeStyle? })string
ordinal(n, { short? })string
year(n, { mode? })string
date(day, month, { year?, style? })string
currency(amount, { cheque?, style? })string
toBnDigits(s), toEnDigits(s)string
SPEC_VERSION, VERSIONstring

Input types

Numbers accept a safe-integer number, a bigint or a string. A number that is not a safe integer (1.5, 2 ** 53, NaN) throws InvalidInputError, so pass decimals and money as strings. Types for every option are exported (WordsOptions, DateOptions …).

In the browser

<script src="https://cdn.jsdelivr.net/npm/bnspellnum@0.1.1/dist/index.global.js"></script>
<script>
  document.title = bnspellnum.words(2026);   // দুই হাজার ছাব্বিশ
</script>

Errors

import { date, OutOfRangeError, BnSpellNumError } from "bnspellnum";

try {
  date(30, 2);
} catch (e) {
  if (e instanceof OutOfRangeError) console.log(e.code, e.message); // "OutOfRange" ...
  // every error is also an instanceof BnSpellNumError
}

More