What is @types/d3-format?
The @types/d3-format package provides TypeScript type definitions for the d3-format module, which is a JavaScript library for formatting numbers for human consumption. It allows developers to format numbers in various ways, such as currency, percentage, precision, and more. The type definitions enable TypeScript developers to use d3-format with type checking and IntelliSense in their IDEs.
What are @types/d3-format's main functionalities?
Number Formatting
Format numbers to a fixed number of decimal places.
import { format } from 'd3-format';
const formatter = format('.2f');
const formattedNumber = formatter(123.456); // '123.46'
Currency Formatting
Format numbers as currency, including symbol, comma separation, and decimal precision.
import { format } from 'd3-format';
const currencyFormatter = format('$,.2f');
const formattedCurrency = currencyFormatter(1234.56); // '$1,234.56'
Percentage Formatting
Format numbers as percentages with rounding and no decimal places.
import { format } from 'd3-format';
const percentFormatter = format('.0%');
const formattedPercent = percentFormatter(0.123); // '12%'
SI Prefix Formatting
Format numbers using SI prefixes with a specified precision.
import { format } from 'd3-format';
const siFormatter = format('.3s');
const formattedSI = siFormatter(123456); // '123k'
Other packages similar to @types/d3-format
numeral
Numeral is a standalone library for formatting and manipulating numbers. It offers a wide range of formatting options and is similar to d3-format in its purpose. However, it does not require D3 and has its own syntax for specifying formats.
accounting
Accounting.js is a tiny JavaScript library for number, money and currency formatting, which also offers some basic arithmetic and utility functions. It is similar to d3-format in terms of currency formatting but is more focused on accounting and financial operations.
globalize
Globalize provides number formatting and parsing, currency formatting, and date and time formatting that are culture-aware. It is based on the Unicode Consortium's Common Locale Data Repository (CLDR). It is more comprehensive than d3-format in terms of internationalization support.