1.0.4 • Published 7 months ago

format-number-lib v1.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

format-number-lib

format-number-lib is a lightweight JavaScript library for formatting numbers with thousands separators. It’s simple, efficient, and solves a common problem in formatting numbers for better readability.

Installation

Install the library via npm:

npm install format-number-lib

Usage

Import the library into your project and use it to format numbers.

CommonJS Example:

const formatNumber = require('format-number-lib');

console.log(formatNumber(1234567)); // "1,234,567"

ES Modules Example:

import formatNumber from 'format-number-lib';

console.log(formatNumber(9876543210)); // "9,876,543,210"

API

formatNumber(num)

Formats a number by adding thousands separators.

  • Parameters:
    • num (number): The number to format.
  • Returns:
    • (string): The formatted number with thousands separators.

Example:

formatNumber(1234567); // "1,234,567"
formatNumber(1000000); // "1,000,000"

Error Handling

If the input is not a valid number, the function throws an error:

formatNumber("hello"); // Error: Input must be a number.

Features

  • Lightweight and fast.
  • Handles large numbers effortlessly.
  • Throws errors for invalid inputs.

How it Works

The function uses a regular expression to insert commas at every thousand, starting from the rightmost digit of the number:

num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');

Contributing

Contributions are welcome! If you’d like to improve this library or report an issue, feel free to open a GitHub issue or submit a pull request. Visit the GitHub repository for more details.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Author

Developed by app-tech5. If you find this library helpful, don’t forget to give it a ⭐ on GitHub!

1.0.4

7 months ago

1.0.3

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago