包详细信息

to-words

mastermunj254.6kMIT4.7.0

Converts numbers (including decimal points) into words & currency.

to words, number to words, number-to-words, number to words currency

自述文件

Number to Words

Introduction

Convert numbers (including decimals) into words with multi-locale and currency support. Ideal for invoicing, e-commerce, and financial apps.

Features

  • Number to Words: Convert integers and decimals to text.
  • Currency Support: Easily handle conversions with locale-specific currency options.
  • Multi-Locale: Supports multiple languages and regions.
  • Highly Configurable: Tailor conversion rules to your needs.

Use Cases

  • Financial Applications: Generate amount-in-words for invoices or cheques.
  • E-commerce Platforms: Display totals in words for user receipts.
  • Educational Tools: Teach number systems through text conversions.
  • Localization: Support multiple languages and currencies seamlessly.

Installation

npm install to-words --save

Usage

Importing

const { ToWords } = require('to-words');

OR

import { ToWords } from 'to-words';

Config Options

const toWords = new ToWords({
  localeCode: 'en-IN',
  converterOptions: {
    currency: true,
    ignoreDecimal: false,
    ignoreZeroCurrency: false,
    doNotAddOnly: false,
    currencyOptions: {
      // can be used to override defaults for the selected locale
      name: 'Rupee',
      plural: 'Rupees',
      symbol: '₹',
      fractionalUnit: {
        name: 'Paisa',
        plural: 'Paise',
        symbol: '',
      },
    },
  },
});

Options can be set at instance level, or along with individual call to convert method.

const toWords = new ToWords();

let words = toWords.convert(123);
// words = One Hundred Twenty Three

words = toWords.convert(123.45);
// words = One Hundred Twenty Three Point Fourty Five

words = toWords.convert(123.045);
// words = One Hundred Twenty Three Point Zero Four Five

Note: When fractional part starts with zero, the digits after decimal points are converted into respective numbers individually

To convert to currency

const toWords = new ToWords();

let words = toWords.convert(452, { currency: true });
// words = Four Hundred Fifty Two Rupees Only

words = toWords.convert(452.36, { currency: true });
// words = Four Hundred Fifty Two Rupees And Thirty Six Paise Only

To discard fractional unit

const toWords = new ToWords();

let words = toWords.convert(452.36, { currency: true, ignoreDecimal: true });
// words = Four Hundred Fifty Two Rupees Only

To ignore major currency number when it's zero

const toWords = new ToWords();

let words = toWords.convert(0.572, { currency: true, ignoreZeroCurrency: true });
// words = Five Hundred Seventy Two Paise Only

Options

Option Type Default Description
localeCode string 'en-IN' Locale code for selecting i18n.
currency boolean false Whether the number to be converted into words written as currency.
Note: When currency:true, number will be rounded off to two decimals before converting to words
ignoreDecimal boolean false Whether to ignore fractional unit of number while converting into words.
ignoreZeroCurrency boolean false Whether to ignore zero currency value while converting into words.
doNotAddOnly boolean false Do not add only at the end of the words. This works only when currency = true
currencyOptions object undefined By default currency options are taken from the specified locale.
This option allows to specify different currency options while keeping the language details from the selected locale (e.g. convert to English text but use EUR as a currency). You can define different currencies for each call to convert() so it works also if you need to dynamically support multiple currencies.
This works only when currency = true

Supported Locale

Country Language Locale
India Bengali bn-IN
Estonia Estonian ee-EE
UAE English en-AE
Bangladesh English en-BD
UK English en-GB
Ghana English en-GH
Ireland English en-IE
India English en-IN (default)
Myanmar English en-MM
Mauritius English en-MU
Nigeria English en-NG
Nepal English en-NP
Oman English en-OM
Philippines English en-PH
USA English en-US
Argentina Spanish es-AR
España Spanish es-ES
Mexico Spanish es-MX
Venezuela Spanish es-VE
Iran Persian fa-IR
Belgium French fr-BE
France French fr-FR
India Gujarati gu-IN
India Hindi hi-IN
India Kannada kn-IN
Korean, Republic of Hangul ko-KR
Latvia Latvian lv-LV
India Marathi mr-IN
Suriname Dutch nl-SR
Nepal Nepali np-NP
Brazil Portuguese pt-BR
Turkey Turkish tr-TR
Pakistan Urdu ur-PK

Inspiration for core logic

https://stackoverflow.com/a/46221860

更新日志

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

4.7.0 (2025-07-08)

Features

Bug Fixes

4.6.0 (2025-06-03)

Features

  • add support for singular value. Fix decimal tests (cf838ef)
  • fixed conflicts (ac6fe1f)
  • update README.md to add Latvian locale (d777b2d)

4.5.1 (2025-05-15)

Bug Fixes

  • test: fixed unit test to support Venezuelan Spanish language (e9a3f79)

4.5.0 (2025-04-20)

Features

  • add support for Bengali (a39af4d)
  • locales: add np_NP support (232ec01)

4.4.0 (2025-03-18)

Features

  • add support for Kannada (bdd88ff)

Bug Fixes

4.3.1 (2025-03-14)

4.3.0 (2025-02-08)

Features

  • locales: add Pakistan Urdu language support (93f4dbb)

4.2.0 (2024-12-11)

Features

  • added es-AR format support (5f5ffb6)
  • added files and readme updated (c3c02f5)
  • Argentinian currency added (d28e351)

Bug Fixes

  • pre-commit: added again text (85bdf0b)

4.1.0 (2024-07-17)

Features

  • expose locales in an iterable capacity (cb7b8cf)
  • locales: spanish support added (20b7aa6)

Bug Fixes

  • add optional trailing variant to exactWordsMapping (44fd000)
  • annotate types of test macro entries properly (37c7768)
  • use singular field of CurrencyOptions (566ae94)

4.0.1 (2024-03-12)

Bug Fixes

4.0.0 (2023-12-21)

3.7.0 (2023-11-28)

Features

  • locale: add fr-BE support (85de6d0)

3.6.1 (2023-06-27)

Bug Fixes

3.6.0 (2023-06-19)

Features

  • add support for only text to be in front of number words (e96ca2f)
  • locale: add estonian (ee-EE) language support (df44a3f)

3.5.1 (2023-04-24)

3.5.0 (2023-02-27)

Features

  • locale: add support for en-NP locale (c66759a)

3.4.0 (2023-01-17)

Features

  • locale: add support to pt-BR locale (b4061ef)

3.3.2 (2022-12-16)

3.3.1 (2022-11-10)

Bug Fixes

3.3.0 (2022-08-02)

Features

  • allow overriding currency details per conversion (380831e)

3.2.0 (2022-04-16)

Features

  • language: added support for dutch nl-SR language and currency (#805) (4ac84c3)

3.1.1 (2022-03-28)

Bug Fixes

  • add includes of ignore one for words (a289892)
  • generate build (6d08e8a)

3.1.0 (2022-02-09)

Features

  • GB: added tests for GB support (b9f488f)

3.0.3 (2022-01-06)

Bug Fixes

  • remove deepsource config (0ae7a59)

3.0.2 (2021-11-24)

3.0.1 (2021-10-23)

3.0.0 (2021-09-14)

Features

3.0.0-alpha.0 (2021-09-04)

Features

  • refactor and new languages (ec68e6c)

Bug Fixes

  • remove default class export to keep import syntax same (bc15df6)
  • resolve rebase issue (2d175f2)

2.5.0 (2021-08-24)

2.4.0 (2021-08-06)

Features

2.3.2 (2021-05-24)

2.3.1 (2020-12-08)

Bug Fixes

  • change MMK to follow US numeral systems (#265) (a37d6f6)

2.3.0 (2020-11-20)

Features

2.2.5 (2020-11-08)

2.2.4 (2020-11-08)

2.2.3 (2020-09-25)

2.2.2 (2020-09-22)

2.2.1 (2020-06-20)

Bug Fixes

  • build & add code for en-MU (6e01b2b)

2.2.0 (2020-06-20)

Features

  • add Mauritius English locale (cead7b4)

2.1.1 (2020-05-01)

Bug Fixes

2.1.0 (2020-05-01)

Features

2.0.3 (2020-04-25)

2.0.2 (2020-04-24)

Bug Fixes

2.0.1 (2020-04-23)

2.0.0 (2020-04-23)

⚠ BREAKING CHANGES

  • Rewrote the core into Typescript.

feat: added options ignoreDecimal, ignoreZeroCurrency

feat: added provision for i18n, currently supports en-IN (default) and en-US

fix: wrong conversion of fractional unit beginning with zero

docs: updated README

test: added unit tests with jest

Bug Fixes

* Rewrite in TypeScript with enahcements (9617f26)