easy-currency-format
v1.0.1
Published
A framework-agnostic currency utility package for safe arithmetic and localization.
Maintainers
Readme
💰 easy-currency-format
A modern, reliable JavaScript class for handling monetary values and formatting. It uses BigInt for internal value storage to eliminate floating-point arithmetic errors, ensuring perfect precision in all calculations.
✨ Features
- Error-Free Arithmetic: Uses
BigIntfor all internal value storage and calculations. - Intl.NumberFormat: Leverages the native API for accurate, localized currency formatting.
- Automatic Precision: Maps common country codes (
VN,JP,US) to their standard ISO 4217 currency codes, default locales, and decimal precision (e.g., JPY/VND use 0 decimals). - Flexible Formatting: Supports full formatted strings or separated parts (value, symbol, placement) for custom UI rendering.
⚙️ Installation
npm i easy-currency-format
🛠️ Usage
import Currency from 'easy-currency-format';
// --- Example 1: VND (Zero decimals)
// The input number (500000) is automatically scaled to BigInt
const priceVND = new Currency(500000, 'VN');
// Output: 500.000 ₫
console.log(priceVND.format());
// --- Example 2: USD (Two decimals)
// The input (1234.56) is internally converted to BigInt(123456)
const priceUSD = new Currency(1234.56, 'USD');
// Output: $1,234.56
console.log(priceUSD.format());
// --- Example 3: Custom High Precision
// Explicitly setting 4 decimal places for high-precision trading/crypto
const highPrecision = new Currency(1.23456, 'USD', 4);
// Output: $1.2346 (Note: Intl.NumberFormat handles rounding for display)
console.log(highPrecision.format());