@sashakil12/bdtformat
v0.2.2
Published
Takes a number input and returns a string following bangladeshi monetary amount writing fashion used commonly in writing bank checks across bangladesh. Formats money strings and spells amounts out in words in Bangla or English. Create money string, bangla
Maintainers
Readme
BDT Format
Javascript number to money string and words generator, Bangladeshi format
Formats numbers the way monetary amounts are written across Bangladesh (rightmost three digits, then twos), and spells amounts out in words — in Bangla or English — the way they are written on a bank cheque.
Install
npm i @sashakil12/bdtformat
# or
pnpm add @sashakil12/bdtformatFormatting
const { bdtFormat } = require('@sashakil12/bdtformat')
bdtFormat(152457899) // '15,24,57,899'
bdtFormat(655125.4) // '6,55,125.4'
bdtFormat(-33500) // '-33,500'In words
const { bdtWords } = require('@sashakil12/bdtformat')
bdtWords(1234567.89) // 'বারো লক্ষ চৌত্রিশ হাজার পাঁচ শত সাতষট্টি টাকা ঊননব্বই পয়সা মাত্র'
bdtWords(1234567.89, 'en') // 'taka twelve lakh thirty-four thousand five hundred sixty-seven and eighty-nine poisha only'
bdtWords(100000) // 'এক লক্ষ টাকা মাত্র'
bdtWords(10000000, 'en') // 'taka one crore only'
bdtWords(-500, 'en') // 'minus taka five hundred only'Bangla ('bn') is the default. Pass an options object for finer control:
bdtWords(152, { lang: 'en', currency: false }) // 'one hundred fifty-two'
bdtWords(152, { lang: 'en', only: false }) // 'taka one hundred fifty-two'| option | default | effect |
| ---------- | ------- | ------------------------------------------------- |
| lang | 'bn' | 'bn' or 'en' |
| currency | true | false spells the bare number, no টাকা/পয়সা |
| only | true | false drops the trailing মাত্র / only |
Paisa is taken from the first two decimal places and rounded, so .4 reads as
forty paisa. A rounding carry is handled properly — bdtWords(5.999, 'en') is
'taka six only', never "one hundred poisha".
Big numbers
Both functions accept number, string, and BigInt. Pass large values as a
string or BigInt to keep every digit — a plain number above
Number.MAX_SAFE_INTEGER throws rather than silently losing precision.
bdtFormat('15245789963531444785632') // '15,24,57,89,96,35,31,44,47,85,632'
bdtWords(90000000n, 'en') // 'taka nine crore only'Scale names repeat past crore the way the system actually works — 10¹² is
one lakh crore, 10¹⁴ is one crore crore — so there is no upper bound where
the naming runs out.
Performance
Both functions work on the decimal digit string, never on the numeric value, so
they run in O(n) in the number of digits. The obvious alternative — peeling
groups off with n % 100n and n / 100n — is O(n²), because each BigInt divmod
is itself O(n).
For any real currency amount the cost is flat at roughly 100 ns per digit. (For
absurd inputs of thousands of digits, the words output itself grows quadratically
because the scale name repeats crore — that is inherent to the naming system,
not the algorithm; the generator stays linear in the size of the text it emits.)
Notes
- Invalid input throws: non-numeric types,
NaN, andInfinity. bdtFormatpreserves the fractional part as given;bdtWordsrounds it to paisa.
