components/numbers
import { … } from 'zidane-ui'ui/src/components/numbers.ts
Functions#
isCompactFunction#
text is a short number: it ends with a compact suffix.
function isCompact(text: string): booleanParameters
| Name | Type | Description |
|---|---|---|
| text | string | The formatted number. |
Returns
booleannumberFormatsFunction#
The number formats of locale, made once per locale.
function numberFormats(locale?: string): NumberFormatsParameters
| Name | Type | Default | Description |
|---|---|---|---|
| locale? | string | 'en-US' | A BCP 47 locale. Default |
Returns
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.
function setDecimals(values: readonly number[], max?: number): numberParameters
| Name | Type | Default | Description |
|---|---|---|---|
| values | readonly number[] | The set. | |
| max? | number | 2 | The most decimals to use. |
Returns
numbershareDecimalsFunction#
The decimals of a list of shares (0 to 1), one count for all: 1 when any share is under 10%, else 0.
function shareDecimals(shares: readonly number[]): numberParameters
| Name | Type | Description |
|---|---|---|
| shares | readonly number[] | The shares. |
Returns
numbertrueMinusFunction#
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%).
function trueMinus(text: string): stringParameters
| Name | Type | Description |
|---|---|---|
| text | string | A formatted value. |
Returns
stringunitDecimalsFunction#
setDecimals for a set in unit: money takes 0 or 2 decimals, never 1 ($190.50).
function unitDecimals(values: readonly number[], unit: string | undefined): numberParameters
| Name | Type | Description |
|---|---|---|
| values | readonly number[] | The set. |
| unit | string | undefined | The values' unit. |
Returns
numberwithUnitFunction#
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).
function withUnit(text: string, unit: string | undefined, compact?: boolean): stringParameters
| Name | Type | Default | Description |
|---|---|---|---|
| text | string | The formatted number. | |
| unit | string | undefined | The unit, or nothing. | |
| compact? | boolean | false |
|
Returns
stringInterfaces#
NumberFormatsInterface#
The formats of one locale.
interface NumberFormatsProperties
| Name | Type | Description |
|---|---|---|
| compact | (value: number) => string | A short number with at most 3 significant digits: |
| locale | string | The locale the numbers are written in. |
| number | (value: number, digits?: number, fixed?: boolean) => string | A grouped number: |
| percent | (value: number, digits?: number, fixed?: boolean) => string | A share as a percent: |