Skip to content

An easily extendable library for parsing numeric values into words, either alone or along with currencies in various locales.

License

Notifications You must be signed in to change notification settings

mateuszgrzybek/amount2words

Repository files navigation

amount2words

Tests npm npm bundle size

A simple library for parsing numeric values into words, written in TypeScript.

Use case demo

Description

This library exposes two methods for parsing numeric values into strings of text. You can either use parseValueToWords to parse a single non-decimal value into words, or use the parseDecimalValueToWords function to parse decimal values, along with the supported currencies and locales.

Currently supported currencies:

  • USD - US Dollars
  • PLN - Polish Zloty

Currently supported parsing locales:

  • enUS - American English
  • plPL - Polish

How to use

// CommonJS module
const a2w = require("amount2words");

// ECMAScript Module
import a2w from "amount2words";

// parsing a single numeric value for given locale
const parsedValue = a2w.parseValueToWords("6794", "enUS");
// returns Six Thousand Seven Hundred Ninety-Four

// parsing decimal amount, with currency, for given locale
const parsedDecimalValue = const parsedValue = a2w.parseDecimalValueToWords("334877.99", "USD", "enUS");
// returns Three Hundred Thirty-Four Thousand Eight Hundred Seventy-Seven Dollars and Ninety-Nine Cents

Accepted function parameters

type parseValueToWordsParams = {
  value?: string; // defaults to "0"
  parsingLocale?: string; // defaults to "enUS"
};

type parseDecimalValueToWordsParams = {
  amountToParse: string;
  currencySymbol: string;
  locale: string;
  toLowerCase?: boolean; // defaults to false
};

Acknowledgments

Inspired by Mohsen Alyafei's proposed StackOverflow solution.

About

An easily extendable library for parsing numeric values into words, either alone or along with currencies in various locales.

Topics

Resources

License

Stars

Watchers

Forks