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.

139 lines (96 loc) โ€ข 4.96 kB
# Word-Number-Word ๐Ÿ”ข โ†”๏ธ ๐Ÿ”  [![npm version](https://img.shields.io/npm/v/word-number-word.svg)](https://www.npmjs.com/package/word-number-word) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) [![Types](https://img.shields.io/badge/%F0%9F%93%9D-TypeScript-blue)](https://www.typescriptlang.org/) [![Build](https://img.shields.io/badge/build-passing-brightgreen.svg)](#) > Convert numbers to words and words back to numbers โ€” now with support for currency. Simple, accurate, and type-safe. Works seamlessly with JavaScript, TypeScript, Node.js, React, Next.js, and even from the CLI. --- ## โœจ Features - ๐Ÿ” **Bi-directional conversion** - `42 โ†’ "forty-two"` - `"seven point five"` โ†’ `7.5` - โœ… Supports **integers**, **negatives**, and **decimal numbers** - ๐Ÿ’ฐ Currency support (e.g., `10.25` โ†”๏ธ `"ten dollars and twenty-five cents"`) - ๐ŸŒ Handles numbers up to **trillions** - ๐Ÿ’ป Works with **Node.js**, **browser environments**, and **CLI** - ๐Ÿ“ฆ Built with **TypeScript** & supports **ESM** and **CommonJS** - ๐Ÿ”ง Lightweight and dependency-free --- ## ๐Ÿ“ฆ Installation ```bash npm install word-number-word # or yarn add word-number-word ``` --- ## ๐Ÿš€ Usage ### JavaScript / TypeScript ```ts import { numberToWord, wordToNumber, currencyToWord, wordToCurrency, } from "word-number-word"; // Basic usage console.log(numberToWord(42)); // "forty-two" console.log(wordToNumber("minus three")); // -3 // Currency support console.log(currencyToWord(10.25, "usd")); // "ten dollars and twenty-five cents" console.log(wordToCurrency("ten dollars and five cents", "usd")); // 10.05 ``` ### CLI Usage ```bash npx word-number-word 123 # Output: "one hundred twenty-three" npx word-number-word "two thousand and five" # Output: 2005 ``` --- ## ๐Ÿ“š API Reference | Function | Description | Example | | --------------------------------------- | ---------------------------------------------------------- | --------------------------------------------------------------- | | `numberToWord(num: number)` | Converts a number to its word form | `numberToWord(123)` โ†’ `"one hundred twenty-three"` | | `wordToNumber(str: string)` | Converts a word string into a number | `wordToNumber("five")` โ†’ `5` | | `currencyToWord(num: number, currency)` | Converts currency number to words (with major/minor units) | `currencyToWord(4.5, "usd")` โ†’ `"four dollars and fifty cents"` | | `wordToCurrency(str, currency)` | Converts word form currency to number | `wordToCurrency("ten dollars and five cents", "usd")` โ†’ `10.05` | > **Note:** Currency codes supported `"usd"`, `"bdt"`, `"eur"`, `"gbp"`. Handles both `"forty-two"` and `"forty two"` formats. --- ## ๐ŸŒ Supported Environments - โœ… Node.js (v14+) - โœ… Modern Browsers - โœ… TypeScript & JavaScript - โœ… Frameworks: **React**, **Next.js**, **Express**, **Vite**, etc. - โœ… CLI (via `npx` or install globally) --- ## ๐Ÿงช Examples | Input | Output | | ----------------------------------------------------- | -------------------------------- | | `123` | `"one hundred twenty-three"` | | `-99.5` | `"minus ninety-nine point five"` | | `"fourteen"` | `14` | | `"minus two"` | `-2` | | `"three point one four"` | `3.14` | | `currencyToWord(4.5, "usd")` | `"four dollars and fifty cents"` | | `wordToCurrency("ten dollars and five cents", "usd")` | `10.05` | --- ## ๐Ÿ› ๏ธ CLI Installation (Optional) Install globally for quick conversions in your terminal: ```bash npm install -g word-number-word ``` Then use it anywhere: ```bash word-number-word 456 # โ†’ "four hundred fifty-six" ``` --- ## ๐Ÿ“„ License MIT License ยฉ [Hridoy Saha](https://github.com/hridoysaha969) --- ## ๐Ÿ™Œ Contributing Contributions, suggestions, and improvements are welcome! Feel free to [open an issue](https://github.com/hridoysaha969/word-number-word/issues) or submit a pull request. --- ## ๐Ÿ“ซ Contact For support or feedback, reach out via [GitHub](https://github.com/hridoysaha969/word-number-word) or [LinkedIn](https://www.linkedin.com/in/sahahridoy/).