Catalog npm
Back to packageRELEASE DETAILS

1.0.0

npm

@0fprod/smart-amount

A flexible TypeScript library for formatting numbers as currency, percentages, tokens, and raw values with intelligent small number handling

compact-notationcryptocurrencyfinanceformatterintlnumber-formattingpercentagetokentypescriptweb3
Published4 Aug 2025
Direct dependencies12
Transitive dependenciesNot calculated

README

Current package documentation

Smart Amount

CI npm version License: MIT TypeScript

A flexible TypeScript library for formatting numbers as currency, percentages, tokens, and raw values with intelligent small number handling and multi-currency support.

Installation

npm install @0fprod/smart-amount

Usage

import { format } from '@0fprod/smart-amount';

// Currency formatting with multi-currency support
format(1234.56, { type: 'currency' });
// Output: "$1,234.56"

format(1234.56, { locale: 'en-US', currency: 'EUR' });
// Output: "€1,234.56"

format(1234.56, { locale: 'de-DE', currency: 'EUR' });
// Output: "1.234,56 €"

format(1234.56, { locale: 'en-GB', currency: 'GBP' });
// Output: "£1,234.56"

// Percentage formatting
format(0.12, { type: 'percentage' });
// Output: "0.12%"

format(25.5, { type: 'percentage' });
// Output: "25.50%"

format(1234567, { type: 'percentage', compact: true });
// Output: "123.46M%"

// Token formatting
format(0.00001234, { type: 'token', tokenSymbol: 'ETH' });
// Output: "0.00001234 ETH"

format(1234567, { type: 'token', tokenSymbol: 'BTC', compact: true });
// Output: "1.23M BTC"

// Raw number formatting
format(1234567, { type: 'raw' });
// Output: "1234567"

// Small numbers with intelligent handling
format(0.000000123, { type: 'currency' });
// Output: "$0.000000123"

format(0.000000123, { type: 'token', tokenSymbol: 'ETH' });
// Output: "0.000000123 ETH"

// Compact notation for large numbers
format(1234567, { type: 'currency', compact: true });
// Output: "$1.23M"

format(1234567, { type: 'percentage', compact: true });
// Output: "1.23M%"

// Auto-compact based on threshold
format(1234567, { type: 'currency', autoCompact: true });
// Output: "$1.23M"

format(1000, { type: 'currency', autoCompact: true });
// Output: "$1,000.00"

// Show signs for positive values
format(1234.56, { type: 'currency', showSign: true });
// Output: "+$1,234.56"

format(1234.56, { type: 'percentage', showSign: true });
// Output: "+1,234.56%"

// Custom decimal places
format(1234.56, { type: 'currency', decimals: 0 });
// Output: "$1,235"

format(1234.56, { type: 'percentage', decimals: 3 });
// Output: "1,234.560%"

// Significant digits for small numbers
format(0.0000123, { type: 'currency', significantDigits: 2 });
// Output: "$0.000012"

// Full decimals for tokens (preserves precision)
format("123456789.123456789", { type: 'token', tokenSymbol: 'ETH', fullDecimals: true });
// Output: "123,456,789.123456789 ETH"

Supported Currencies and Locales

The library supports multiple currencies with their respective locales:

// Available currencies:
type CurrencyCode = 'USD' | 'EUR' | 'GBP' | 'AED' | 'INR' | 'NGN';

// Available locales:
type LocaleCode = 'en-US' | 'en-GB' | 'de-DE' | 'es-ES' | 'fr-FR' | 'it-IT' | 'ar-AE' | 'hi-IN' | 'en-NG';

// Examples:
format(1234.56, { locale: 'en-US', currency: 'USD' });  // $1,234.56
format(1234.56, { locale: 'en-US', currency: 'EUR' });  // €1,234.56
format(1234.56, { locale: 'de-DE', currency: 'EUR' });  // 1.234,56 €
format(1234.56, { locale: 'en-GB', currency: 'GBP' });  // £1,234.56
format(1234.56, { locale: 'en-US', currency: 'AED' });  // AED 1,234.56
format(1234.56, { locale: 'en-US', currency: 'INR' });  // ₹1,234.56
format(1234.56, { locale: 'en-US', currency: 'NGN' });  // NGN 1,234.56

Features

  • Multiple format types: Currency, percentage, token, and raw number formatting
  • Multi-currency support: USD, EUR, GBP, AED, INR, NGN with proper locale formatting
  • Intelligent small number handling: Automatically switches to scientific notation for very small numbers
  • Compact notation: K, M, B suffixes for large numbers
  • Auto-compact: Automatically applies compact notation based on threshold
  • Sign display: Option to show + sign for positive values
  • Custom precision: Configurable decimal places and significant digits
  • Full decimals: Preserve full precision for token amounts
  • Internationalization support: Uses Intl API for locale-aware formatting
  • TypeScript support: Full type definitions included
  • Zero dependencies: Lightweight and fast
  • Comprehensive testing: 80+ tests covering all functionality
  • Modular architecture: Clean separation of concerns with dedicated formatters

Project Structure

The library is organized with a clean, modular architecture:

src/
├── index.ts              # Main entry point (re-exports everything)
├── types.ts              # TypeScript interfaces and types
├── constants.ts          # Constants and configurations
├── utils.ts              # Utility functions
├── formatters/
│   ├── index.ts          # Main format function
│   ├── currency.ts       # Currency formatting logic
│   ├── percentage.ts     # Percentage formatting logic
│   ├── token.ts          # Token formatting logic
│   └── raw.ts           # Raw number formatting logic
└── helpers/
    └── small-numbers.ts  # Small number handling logic

API

format(value: number | string, options?: FormatNumbersOptions): string

Formats a number according to the specified options.

Options

  • type: The format type ('currency', 'percentage', 'token', 'raw')
  • locale: Locale code for formatting (e.g., 'en-US', 'de-DE')
  • currency: Currency code for currency formatting (e.g., 'USD', 'EUR')
  • decimals: Number of decimal places (default: 2)
  • compact: Use compact notation for large numbers
  • autoCompact: Automatically apply compact notation based on threshold
  • compactThreshold: Threshold for auto-compact (default: 10000)
  • tokenSymbol: Token symbol for token formatting
  • significantDigits: Number of significant digits for small numbers
  • showSign: Show + sign for positive values and - for negative values
  • rounded: Round small numbers to zero
  • fullDecimals: Preserve full decimal precision for tokens

Development

Running Tests

npm test              # Run all tests
npm run test:watch    # Run tests in watch mode
npm run test:coverage # Run tests with coverage report

Building

npm run build         # Build for production
npm run dev           # Build in watch mode

License

MIT

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 12

Native requirements
12 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
jest
Environment
{
  "local_name": "jest",
  "alias": false,
  "peer_metadata": null
}
^29.7.0development
husky
Environment
{
  "local_name": "husky",
  "alias": false,
  "peer_metadata": null
}
^9.1.7development
eslint
Environment
{
  "local_name": "eslint",
  "alias": false,
  "peer_metadata": null
}
^8.54.0development
ts-jest
Environment
{
  "local_name": "ts-jest",
  "alias": false,
  "peer_metadata": null
}
^29.1.1development
prettier
Environment
{
  "local_name": "prettier",
  "alias": false,
  "peer_metadata": null
}
^3.6.2development
typescript
Environment
{
  "local_name": "typescript",
  "alias": false,
  "peer_metadata": null
}
^5.3.2development
@types/jest
Environment
{
  "local_name": "@types/jest",
  "alias": false,
  "peer_metadata": null
}
^29.5.8development
lint-staged
Environment
{
  "local_name": "lint-staged",
  "alias": false,
  "peer_metadata": null
}
^16.1.4development
@commitlint/cli
Environment
{
  "local_name": "@commitlint/cli",
  "alias": false,
  "peer_metadata": null
}
^19.8.1development
@typescript-eslint/parser
Environment
{
  "local_name": "@typescript-eslint/parser",
  "alias": false,
  "peer_metadata": null
}
^6.12.0development
@commitlint/config-conventional
Environment
{
  "local_name": "@commitlint/config-conventional",
  "alias": false,
  "peer_metadata": null
}
^19.8.1development
@typescript-eslint/eslint-plugin
Environment
{
  "local_name": "@typescript-eslint/eslint-plugin",
  "alias": false,
  "peer_metadata": null
}
^6.12.0development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "shasum": "a44ecbdd99bf559b428364aeea56b568fcdac212",
  "tarball": "https://registry.npmjs.org/@0fprod/smart-amount/-/smart-amount-1.0.0.tgz",
  "fileCount": 36,
  "integrity": "sha512-uSw13/Xve6Ynby2BG7XRxuCFblJmU5Zzv96s0BI8krG/M/zmVMVGraEAGKqUj9WMPosYLPqnGF7R2oYy+0NMeA==",
  "signatures": [
    {
      "sig": "MEYCIQD27p9ZSxojA9Jh0qv3SQKZU7WU6xmOkXqVf8uJ3UNxngIhAIaNIXO5nMw5eU4FurEiI16Pw+kVkrPL1tbJoUx9QlAE",
      "keyid": "SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"
    }
  ],
  "unpackedSize": 30997
}

Repository

PENDING
0fprod/smart-amount

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 22:10 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:8052b6c8fb5b29cc6ee1ff593e557dca630309a95b4d0459ca5cfda9d430c9e7

Other collected versions

2.0.05 AugView all collected versions →