menew-currency-formatter
v1.0.3-alpha
Published
A flexible and highly configurable **currency formatter** for multiple locales and styles. Supports prefix/suffix symbols, custom grouping patterns, decimal precision, and more.
Downloads
92
Maintainers
Readme
CurrencyFormatter
A flexible and highly configurable currency formatter for multiple locales and styles. Supports prefix/suffix symbols, custom grouping patterns, decimal precision, and more.
Features
- Format currencies with prefix or suffix symbols.
- Support for custom grouping patterns (e.g., 3-digit, 3,2-digit for India).
- Configurable decimal separators and group separators.
- Show/hide currency symbols and decimal places.
- Predefined formats for major world currencies.
- Fully TypeScript compatible.
Installation
npm install @your-org/currency-formatterUsage
Import
import { CurrencyFormatter, CurrencyFormatName } from "@your-org/currency-formatter";Predefined currency formats
const usdFormatter = CurrencyFormatter.predefinedFormat(CurrencyFormatName.USD);
console.log(usdFormatter.formatWithCurrencyAndDecimal(1234567.89));
// Output: $1,234,567.89Custom formats
const customFormatter = CurrencyFormatter.customFormat({
currency_code: "¤",
group_separator: " ",
decimal_separator: ",",
group_pattern: "3",
currency_position: "suffix",
space_after_currency: true,
});
console.log(customFormatter.formatWithCurrencyAndDecimal(1234567.89));
// Output: 1 234 567,89 ¤Formatting options
| Option | Description |
| ----------------------- | ----------------------------------------- |
| showCurrency | Show/hide currency symbol (default: true) |
| showDecimal | Show/hide decimal places (default: true) |
| decimalPlaces | Number of decimal places to display |
| preserveDecimalPlaces | Keep original decimal places of input |
Predefined Currencies and Format Patterns
| Currency | Format Pattern | Description | Example | | -------- | ---------------------------------- | -------------------------------------------------------------------- | -------------- | | USD | G_3_COMMA_DOT_PREFIX_NO_SPACE | Prefix, 3-digit grouping, comma separator, dot decimal, no space | $1,234.56 | | CAD | G_3_COMMA_DOT_PREFIX_NO_SPACE | Prefix, 3-digit grouping, comma separator, dot decimal, no space | C$1,234.56 | | AUD | G_3_COMMA_DOT_PREFIX_NO_SPACE | Prefix, 3-digit grouping, comma separator, dot decimal, no space | A$1,234.56 | | EUR | G_3_DOT_COMMA_SUFFIX_SPACE | Suffix, 3-digit grouping, dot separator, comma decimal, with space | 1.234,56 € | | GBP | G_3_COMMA_DOT_PREFIX_NO_SPACE | Prefix, 3-digit grouping, comma separator, dot decimal, no space | £1,234.56 | | CHF | G_3_COMMA_DOT_PREFIX_NO_SPACE | Prefix, 3-digit grouping, comma separator, dot decimal, no space | CHF1,234.56 | | JPY | G_NO_DECIMAL_COMMA_PREFIX_NO_SPACE | Prefix, 3-digit grouping, comma separator, no decimals | ¥1,234 | | CNY | G_3_COMMA_DOT_PREFIX_NO_SPACE | Prefix, 3-digit grouping, comma separator, dot decimal, no space | ¥C1,234.56 | | INR | G_3_2_COMMA_DOT_PREFIX_SPACE | Prefix, 3,2-digit grouping, comma separator, dot decimal, with space | ₹ 12,34,567.89 | | AED | G_3_COMMA_DOT_PREFIX_SPACE | Prefix, 3-digit grouping, comma separator, dot decimal, with space | د.إ 1,234.56 | | SAR | G_3_COMMA_DOT_PREFIX_SPACE | Prefix, 3-digit grouping, comma separator, dot decimal, with space | ﷼ 1,234.56 | | OMR | G_3_COMMA_DOT_PREFIX_SPACE | Prefix, 3-digit grouping, comma separator, dot decimal, with space | ر.ع. 1,234.56 | | ZAR | G_3_SPACE_COMMA_PREFIX_SPACE | Prefix, 3-digit grouping, space separator, comma decimal, with space | R 1 234,56 | | XOF | G_3_SPACE_COMMA_PREFIX_SPACE | Prefix, 3-digit grouping, space separator, comma decimal, with space | ₣ 1 234,56 | | DEFAULT | DEFAULT | Prefix, 3-digit grouping, comma separator, dot decimal, no space | $1,234.56 | | STANDARD | NONE | Prefix, no grouping, comma separator, dot decimal | 1234.56 |
License
MIT © Demenew
