UNPKG

numify

Version:

Shortern long numbers to Human Readable K M B format

118 lines (117 loc) 3.48 kB
/** * Supported locale codes for number formatting * - 'en': English (1,234.56) * - 'de': German (1.234,56) * - 'fr': French (1 234,56) * - 'es': Spanish (1.234,56) * - 'in': Indian (1,234.56 with Indian numbering) * - 'it': Italian (1.234,56) * - 'ch': Swiss (1'234.56) * - 'se': Swedish (1 234,56) */ export type LocaleCode = 'en' | 'de' | 'fr' | 'es' | 'in' | 'it' | 'ch' | 'se'; /** * Supported number systems * - 'international': Uses K, M, B, T suffixes (1K, 1M, 1B, 1T) * - 'indian': Uses Indian number system suffixes (K, L, Cr) */ export type SystemType = 'international' | 'indian'; /** * Supported formatting styles * - 'short': Shows short format (1k, 1M, 1B, 1T) * - 'long': Shows long format (1 thousand, 1 million, 1 billion, 1 trillion) */ export type FormatStyle = 'short' | 'long'; /** * Combined type for all supported format types */ export type FormatType = LocaleCode | SystemType; /** * Options for numify function */ export interface NumifyOptions { /** * The locale or number system to use * @example 'en' for English format * @example 'indian' for Indian number system */ formatType?: FormatType; /** * Whether to show decimal places * @default false */ precise?: boolean; /** * Format style to use * @default 'short' */ style?: FormatStyle; /** * Custom units to use for formatting * @example ["Bit", "Bits"] */ units?: string[]; } /** * Options for formatNumber function */ export interface FormatNumberOptions { /** * The locale or number system to use * @example 'en' for English format * @example 'de' for German format */ formatType?: FormatType; } /** * Converts a number to a human readable format with suffixes * @param num - The number to convert * @param options - Configuration options * @returns The formatted number as a string * * @example International format (default) * ```typescript * numify(1234) // "1.23k" * numify(1234567) // "1.23M" * numify(1234567890) // "1.23B" * ``` * * @example Indian format * ```typescript * numify(1234, { formatType: 'in' }) // "1.23K" * numify(123456, { formatType: 'in' }) // "1.23L" * numify(12345678, { formatType: 'in' }) // "1.23Cr" * ``` * * @example With precise option * ```typescript * numify(1234, { precise: true }) // "1.234k" * ``` * * @example With long format * ```typescript * numify(1234, { style: 'long' }) // "1.23 thousand" * numify(1234567, { style: 'long' }) // "1.23 million" * ``` * * @example With custom units * ```typescript * numify(1234, { units: ["Bit", "Bits"] }) // "1.23 Bits" * ``` */ export declare const numify: (num: number, options?: NumifyOptions) => string; /** * Formats a number with thousand separators according to locale * @param num - The number to format * @param options - Configuration options * @returns The formatted number as a string * * @example Different locales * ```typescript * formatNumber(1234567.89) // "1,234,567.89" (en) * formatNumber(1234567.89, { formatType: 'de' }) // "1.234.567,89" * formatNumber(1234567.89, { formatType: 'fr' }) // "1 234 567,89" * formatNumber(1234567.89, { formatType: 'ch' }) // "1'234'567.89" * ``` */ export declare const formatNumber: (num: number, options?: FormatNumberOptions) => string;