Skip to content

components/numbers

import { … } from 'zidane-ui'ui/src/components/numbers.ts

Functions#

isCompactFunction#

text is a short number: it ends with a compact suffix.

TypeScript
function isCompact(text: string): boolean
Parameters
NameTypeDescription
textstring

The formatted number.

boolean

ui/src/components/numbers.ts:171

numberFormatsFunction#

The number formats of locale, made once per locale.

TypeScript
function numberFormats(locale?: string): NumberFormats
Parameters
NameTypeDefaultDescription
locale?string'en-US'

A BCP 47 locale. Default en-US.

ui/src/components/numbers.ts:34

setDecimalsFunction#

One decimal count for a set of values (a column, a series, a stat row, NU5): the fewest, up to max, that write every value as given, so no value is rounded away from the data, no non-zero value reads as zero and no two different values read the same.

TypeScript
function setDecimals(values: readonly number[], max?: number): number
Parameters
NameTypeDefaultDescription
valuesreadonly number[]

The set.

max?number2

The most decimals to use.

number

ui/src/components/numbers.ts:92

shareDecimalsFunction#

The decimals of a list of shares (0 to 1), one count for all: 1 when any share is under 10%, else 0.

TypeScript
function shareDecimals(shares: readonly number[]): number
Parameters
NameTypeDescription
sharesreadonly number[]

The shares.

number

ui/src/components/numbers.ts:127

trueMinusFunction#

A value's minus sign as U+2212, the one a reader sees as a minus, when the text starts with a hyphen before a digit (-52% to −52%).

TypeScript
function trueMinus(text: string): string
Parameters
NameTypeDescription
textstring

A formatted value.

string

ui/src/components/numbers.ts:137

unitDecimalsFunction#

setDecimals for a set in unit: money takes 0 or 2 decimals, never 1 ($190.50).

TypeScript
function unitDecimals(values: readonly number[], unit: string | undefined): number
Parameters
NameTypeDescription
valuesreadonly number[]

The set.

unitstring | undefined

The values' unit.

number

ui/src/components/numbers.ts:116

withUnitFunction#

A formatted number with its unit, by one rule: currency symbols go before the number ($12, -$3), percent signs stick to it (12%), any other unit follows after a space that never breaks (1,882 ms), a thin one after a short number (1.5k ms).

TypeScript
function withUnit(text: string, unit: string | undefined, compact?: boolean): string
Parameters
NameTypeDefaultDescription
textstring

The formatted number.

unitstring | undefined

The unit, or nothing.

compact?booleanfalse

text is a short number (1.5k).

string

ui/src/components/numbers.ts:154

Interfaces#

NumberFormatsInterface#

The formats of one locale.

TypeScript
interface NumberFormats
NumberFormats properties
NameTypeDescription
compact(value: number) => string

A short number with at most 3 significant digits: 950, 1.5k, 48.2k, 4.81M, 2B.

localestring

The locale the numbers are written in.

number(value: number, digits?: number, fixed?: boolean) => string

A grouped number: 1,234.5. At most digits decimals (default 2), exactly digits with fixed.

percent(value: number, digits?: number, fixed?: boolean) => string

A share as a percent: 0.123 is 12.3%. At most digits decimals (default 1), exactly digits with fixed.

ui/src/components/numbers.ts:9