Observed since 20 Oct, 22:10 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
@0fprod/smart-amount
A flexible TypeScript library for formatting numbers as currency, percentages, tokens, and raw values with intelligent small number handling
A flexible TypeScript library for formatting numbers as currency, percentages, tokens, and raw values with intelligent small number handling and multi-currency support.
npm install @0fprod/smart-amount
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"
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
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
format(value: number | string, options?: FormatNumbersOptions): stringFormats a number according to the specified 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 numbersautoCompact: Automatically apply compact notation based on thresholdcompactThreshold: Threshold for auto-compact (default: 10000)tokenSymbol: Token symbol for token formattingsignificantDigits: Number of significant digits for small numbersshowSign: Show + sign for positive values and - for negative valuesrounded: Round small numbers to zerofullDecimals: Preserve full decimal precision for tokensnpm test # Run all tests
npm run test:watch # Run tests in watch mode
npm run test:coverage # Run tests with coverage report
npm run build # Build for production
npm run dev # Build in watch mode
MIT
Download counts and popularity will appear here as the collector receives them.
| Package / target | Requirement | Relationship |
|---|---|---|
jestEnvironment{
"local_name": "jest",
"alias": false,
"peer_metadata": null
} | ^29.7.0 | development |
huskyEnvironment{
"local_name": "husky",
"alias": false,
"peer_metadata": null
} | ^9.1.7 | development |
eslintEnvironment{
"local_name": "eslint",
"alias": false,
"peer_metadata": null
} | ^8.54.0 | development |
ts-jestEnvironment{
"local_name": "ts-jest",
"alias": false,
"peer_metadata": null
} | ^29.1.1 | development |
prettierEnvironment{
"local_name": "prettier",
"alias": false,
"peer_metadata": null
} | ^3.6.2 | development |
typescriptEnvironment{
"local_name": "typescript",
"alias": false,
"peer_metadata": null
} | ^5.3.2 | development |
@types/jestEnvironment{
"local_name": "@types/jest",
"alias": false,
"peer_metadata": null
} | ^29.5.8 | development |
lint-stagedEnvironment{
"local_name": "lint-staged",
"alias": false,
"peer_metadata": null
} | ^16.1.4 | development |
@commitlint/cliEnvironment{
"local_name": "@commitlint/cli",
"alias": false,
"peer_metadata": null
} | ^19.8.1 | development |
@typescript-eslint/parserEnvironment{
"local_name": "@typescript-eslint/parser",
"alias": false,
"peer_metadata": null
} | ^6.12.0 | development |
@commitlint/config-conventionalEnvironment{
"local_name": "@commitlint/config-conventional",
"alias": false,
"peer_metadata": null
} | ^19.8.1 | development |
@typescript-eslint/eslint-pluginEnvironment{
"local_name": "@typescript-eslint/eslint-plugin",
"alias": false,
"peer_metadata": null
} | ^6.12.0 | development |
Source distribution
{
"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
}Observed since 20 Oct, 22:10 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package_version:8052b6c8fb5b29cc6ee1ff593e557dca630309a95b4d0459ca5cfda9d430c9e7