convert-number-to-words
v1.0.3
Published
A utility to convert numbers to English words, including decimal points.
Downloads
355
Maintainers
Keywords
Readme
convert-number-to-words
convert-number-to-words
is a utility to convert numbers to English words, including support for decimal points. This package is perfect for applications requiring number-to-text conversion, such as check-writing software, text-based games, or educational tools.
Features
- Converts integers to English words.
- Supports decimal points.
- Handles negative numbers.
- Provides easy-to-use TypeScript definitions.
Table of Contents
Installation
Install the package via npm:
npm install convert-number-to-words
Or using yarn:
yarn add convert-number-to-words
Usage
Import and use the numberToWords function in your project:
const { numberToWords } = require('convert-number-to-words');
console.log(numberToWords(123)); // Output: "one hundred twenty three"
console.log(numberToWords(-456.78)); // Output: "negative four hundred fifty six point seven eight"
console.log(numberToWords(0.123)); // Output: "zero point one two three"
import { numberToWords } from 'convert-number-to-words';
console.log(numberToWords(123)); // Output: "one hundred twenty three"
console.log(numberToWords(-456.78)); // Output: "negative four hundred fifty six point seven eight"
console.log(numberToWords(0.123)); // Output: "zero point one two three"
Examples
Here are some examples demonstrating the use of convert-number-to-words:
Basic Usage
import { numberToWords } from 'convert-number-to-words';
const num = 1234;
console.log(numberToWords(num)); // Output: "one thousand two hundred thirty four"
Handling Decimals
import { numberToWords } from 'convert-number-to-words';
const num = 45.67;
console.log(numberToWords(num)); // Output: "forty five point six seven"
Negative Numbers
import { numberToWords } from 'convert-number-to-words';
const num = -789;
console.log(numberToWords(num)); // Output: "negative seven hundred eighty nine"
API
numberToWords(num: number): string
Converts a number to its English word representation.
- Parameters:
num
:number
- The number to convert.- Returns:
string
- The English word representation of the number.
Examples
import { numberToWords } from 'convert-number-to-words';
// Convert a positive number
console.log(numberToWords(456)); // Output: "four hundred fifty six"
// Convert a negative number
console.log(numberToWords(-789.12)); // Output: "negative seven hundred eighty nine point one two"
// Convert zero
console.log(numberToWords(0)); // Output: "zero"
Contributing
Contributions are welcome! For feature requests and bug reports, please submit an issue. If you would like to contribute code, please follow the steps below:
- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeature
). - Commit your changes (
git commit -am 'Add new feature'
). - Push to the branch (
git push origin feature/YourFeature
). - Submit a pull request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
- Mohammad Sayem - Github
Acknowledgments
- Inspired by various number-to-words conversion libraries and open-source projects.