1.0.1 • Published 8 months ago

intl-number-formatter v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

IntlFormatter

A flexible and robust number formatting library that provides a consistent interface for formatting numbers across different locales and formats. It gracefully handles browser compatibility issues and provides fallbacks for unsupported features.

Installation

npm install intl-number-formatter

Basic Usage

import { IntlFormatter } from 'intl-formatter';
// Create a formatter instance with default options
const formatter = new IntlFormatter();
// Basic number formatting
formatter.format(1234.567); // "1,234.57"
// Currency formatting
formatter.formatCurrency(1234.567, { currency: 'USD' }); // "$1,234.57"
// Percentage formatting
formatter.formatPercent(0.1234); // "12.3%"

Features

  • Locale and Format Fallbacks: Automatically falls back to a more basic format if the locale and format are not supported.
  • Flexible Options: Provides a wide range of options to customize the formatting style and precision.
  • Browser Compatibility: Uses Intl.NumberFormat under the hood, ensuring compatibility with modern browsers.

Available Formats

  • Currency: Formats a number as a currency string.
  • Decimal: Formats a number as a decimal string.
  • Percent: Formats a number as a percentage string.
  • Scientific: Formats a number in scientific notation.
  • Unit: Formats a number as a unit string.
  • Custom: Formats a number as a custom string.

Available Units

  • Mass: Formats a number as a mass unit (e.g. grams, kilograms).
  • Time: Formats a number as a time unit (e.g. seconds, minutes, hours).
  • Length: Formats a number as a length unit (e.g. meters, kilometers).
  • Volume: Formats a number as a volume unit (e.g. liters, milliliters).
  • Speed: Formats a number as a speed unit (e.g. meters per second, kilometers per hour).
  • Temperature: Formats a number as a temperature unit (e.g. Celsius, Fahrenheit).

Format currency

const formatter = new IntlFormatter();
formatter.formatCurrency(1234.567, { currency: 'USD' }); // "$1,234.57"

Format percent

formatter.formatPercent(0.1234); // "12.3%"

Format unit

formatter.formatUnit(1234.567, { unit: 'meter' }); // "1,234.57 m"

Format custom

formatter.formatCustom(1234.567, (formattedValue) => `${formattedValue} meters`); // "1,234.57 meters"

Format range

formatter.formatRange(1234.567, 1234.567); // "1,234.57 - 1,234.57"

Format compact

formatter.formatCompact(1234.567); // "1.23K"

Format scientific

formatter.formatScientific(1234.567); // "1.234567E3"

Unit Long and Narrow Form

formatter.getUnitLongForm('meter'); // "meter"
formatter.getUnitNarrowForm('meter'); // "m"

Available Options

OptionTypeDefaultDescriptionAvailable Options
localestringnavigator.language or 'en-US'The locale to use for formattingAny valid locale string (e.g. 'en-US', 'fr-FR', 'de-DE')
stylestring'decimal'The formatting style'decimal', 'currency', 'percent', 'unit'
notationstring'standard'The notation to use'standard', 'scientific', 'engineering', 'compact'
minimumFractionDigitsnumber0Minimum number of fraction digits to use0-20
maximumFractionDigitsnumber2Maximum number of fraction digits to use0-20
minimumSignificantDigitsnumberundefinedMinimum number of significant digits1-21
maximumSignificantDigitsnumberundefinedMaximum number of significant digits1-21
useGroupingboolean | stringtrueWhether to use grouping separatorstrue, false, 'always', 'auto', 'min2'
roundingModestring'halfEven'The rounding mode to use'ceil', 'floor', 'expand', 'trunc', 'halfCeil', 'halfFloor', 'halfExpand', 'halfTrunc', 'halfEven'
currencystringundefinedThe currency codeAny valid ISO 4217 currency code (e.g. 'USD', 'EUR', 'JPY')
currencyDisplaystring'symbol'How to display the currency'symbol', 'narrowSymbol', 'code', 'name'
currencySignstringundefinedThe currency sign to use'standard', 'accounting'
unitstringundefinedThe unit to use for unit formattingAny valid unit identifier (e.g. 'meter', 'liter', 'celsius')
unitDisplaystring'short'How to display the unit'short', 'long', 'narrow'
signDisplaystring'auto'When to display the sign'auto', 'never', 'always', 'exceptZero'
compactDisplaystring'short'Compact notation display style'short', 'long'

License

This project is licensed under the MIT License. See the LICENSE file for details.

1.0.1

8 months ago

1.0.0

8 months ago