Home >Web Front-end >JS Tutorial >Simplify Numbers with Aveta: A Handy JavaScript Library

Simplify Numbers with Aveta: A Handy JavaScript Library

Susan Sarandon
Susan SarandonOriginal
2024-12-25 14:05:16317browse

Have you ever wanted to convert large numbers into concise, human-readable formats in your JavaScript projects? Say hello to Aveta—a lightweight library that makes formatting numbers effortless!

Simplify Numbers with Aveta: A Handy JavaScript Library

Why Aveta?

Aveta helps you turn clunky numbers into simple, human-readable formats like 10K, 1.2M, or 3.45B. It's perfect for dashboards, charts, and anywhere clean data presentation is essential.

Input vs. Output

Input Output
6000 '6K'
10000 '10K'
42500 '42.5K'
1250000 '1.25M'
Input

Output

6000 '6K'
10000 '10K'
42500 '42.5K'
1250000 '1.25M'


Installation

# With npm
npm install aveta

# With Yarn
yarn add aveta
Add Aveta to your project using npm or Yarn:

How to Use Aveta

Aveta is flexible and supports various formatting options. Here's a quick overview:
import aveta from 'aveta';

console.log(aveta(8700)); // '8.7K'

Basic Usage


Advanced Options

import aveta from 'aveta';

// Customize digits and lowercase units
console.log(
  aveta(123456, {
    digits: 3,
    lowercase: true,
  })
); // '123k'

// Adjust precision and add a separator
console.log(
  aveta(2048000, {
    precision: 2,
    separator: ',',
  })
); // '2,048M'

// Use custom units
console.log(
  aveta(1440000, {
    units: ['B', 'KB', 'MB', 'GB', 'TB'],
    space: true,
  })
); // '1.44 MB'
Aveta offers customization for precision, separator style, unit case, and more:


Reverse Formatting with AvetaReverse

import { avetaReverse } from 'aveta';

console.log(avetaReverse('8.7K')); // 8700
console.log(avetaReverse('4.57k')); // 4570
console.log(avetaReverse('2.48m')); // 2480000
Need to convert a human-readable number back into its original form? Use avetaReverse:


Command-Line Interface

$ aveta 234000
234K

# Or use npx
$ npx aveta 1234567
1.23M
Run Aveta directly in your terminal:


aveta --help
For CLI options, type:

  1. Why Choose Aveta?
  2. Lightweight: Add minimal overhead to your project.
  3. Customizable: Tailor formats to your specific needs.
  4. User-Friendly: CLI support makes quick testing a breeze.
Comprehensive:

Both forward and reverse formatting options.

This library is crafted with ❤️ by Me.
Contributions are welcome—join us on GitHub to make Aveta even better!

The above is the detailed content of Simplify Numbers with Aveta: A Handy JavaScript Library. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn