2023-11-15 12:41:53 +01:00
|
|
|
import type { ValueOf } from '../types/util';
|
2020-07-06 14:27:32 +02:00
|
|
|
|
|
|
|
export const DECIMAL_UNITS = Object.freeze({
|
|
|
|
ONE: 1,
|
|
|
|
TEN: 10,
|
2023-05-03 11:43:29 +02:00
|
|
|
HUNDRED: 100,
|
|
|
|
THOUSAND: 1_000,
|
|
|
|
MILLION: 1_000_000,
|
|
|
|
BILLION: 1_000_000_000,
|
|
|
|
TRILLION: 1_000_000_000_000,
|
2020-07-06 14:27:32 +02:00
|
|
|
});
|
2023-05-03 11:43:29 +02:00
|
|
|
export type DecimalUnits = ValueOf<typeof DECIMAL_UNITS>;
|
2020-07-06 14:27:32 +02:00
|
|
|
|
|
|
|
const TEN_THOUSAND = DECIMAL_UNITS.THOUSAND * 10;
|
|
|
|
const TEN_MILLIONS = DECIMAL_UNITS.MILLION * 10;
|
|
|
|
|
2023-05-31 09:27:21 +02:00
|
|
|
export type ShortNumber = [number, DecimalUnits, 0 | 1]; // Array of: shorten number, unit of shorten number and maximum fraction digits
|
|
|
|
|
2020-07-06 14:27:32 +02:00
|
|
|
/**
|
2023-05-31 09:27:21 +02:00
|
|
|
* @param sourceNumber Number to convert to short number
|
|
|
|
* @returns Calculated short number
|
2020-07-06 14:27:32 +02:00
|
|
|
* @example
|
|
|
|
* shortNumber(5936);
|
|
|
|
* // => [5.936, 1000, 1]
|
|
|
|
*/
|
2023-05-03 11:43:29 +02:00
|
|
|
export function toShortNumber(sourceNumber: number): ShortNumber {
|
2020-07-06 14:27:32 +02:00
|
|
|
if (sourceNumber < DECIMAL_UNITS.THOUSAND) {
|
|
|
|
return [sourceNumber, DECIMAL_UNITS.ONE, 0];
|
|
|
|
} else if (sourceNumber < DECIMAL_UNITS.MILLION) {
|
|
|
|
return [
|
|
|
|
sourceNumber / DECIMAL_UNITS.THOUSAND,
|
|
|
|
DECIMAL_UNITS.THOUSAND,
|
|
|
|
sourceNumber < TEN_THOUSAND ? 1 : 0,
|
|
|
|
];
|
|
|
|
} else if (sourceNumber < DECIMAL_UNITS.BILLION) {
|
|
|
|
return [
|
|
|
|
sourceNumber / DECIMAL_UNITS.MILLION,
|
|
|
|
DECIMAL_UNITS.MILLION,
|
|
|
|
sourceNumber < TEN_MILLIONS ? 1 : 0,
|
|
|
|
];
|
|
|
|
} else if (sourceNumber < DECIMAL_UNITS.TRILLION) {
|
2023-05-09 23:41:18 +02:00
|
|
|
return [sourceNumber / DECIMAL_UNITS.BILLION, DECIMAL_UNITS.BILLION, 0];
|
2018-08-20 11:29:42 +02:00
|
|
|
}
|
2020-07-06 14:27:32 +02:00
|
|
|
|
|
|
|
return [sourceNumber, DECIMAL_UNITS.ONE, 0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-31 09:27:21 +02:00
|
|
|
* @param sourceNumber Original number that is shortened
|
|
|
|
* @param division The scale in which short number is displayed
|
|
|
|
* @returns Number that can be used for plurals when short form used
|
2020-07-06 14:27:32 +02:00
|
|
|
* @example
|
|
|
|
* pluralReady(1793, DECIMAL_UNITS.THOUSAND)
|
|
|
|
* // => 1790
|
|
|
|
*/
|
2023-05-09 23:41:18 +02:00
|
|
|
export function pluralReady(
|
|
|
|
sourceNumber: number,
|
2023-07-13 11:49:16 +02:00
|
|
|
division: DecimalUnits | null,
|
2023-05-09 23:41:18 +02:00
|
|
|
): number {
|
2020-07-06 14:27:32 +02:00
|
|
|
if (division == null || division < DECIMAL_UNITS.HUNDRED) {
|
|
|
|
return sourceNumber;
|
|
|
|
}
|
|
|
|
|
2023-05-03 11:43:29 +02:00
|
|
|
const closestScale = division / DECIMAL_UNITS.TEN;
|
2020-07-06 14:27:32 +02:00
|
|
|
|
|
|
|
return Math.trunc(sourceNumber / closestScale) * closestScale;
|
|
|
|
}
|
2021-10-14 20:44:59 +02:00
|
|
|
|
2023-05-03 11:43:29 +02:00
|
|
|
export function roundTo10(num: number): number {
|
2021-10-14 20:44:59 +02:00
|
|
|
return Math.round(num * 0.1) / 0.1;
|
|
|
|
}
|
2024-03-11 16:02:21 +01:00
|
|
|
|
|
|
|
export function toCappedNumber(num: string): string {
|
|
|
|
if (parseInt(num) > 99) {
|
|
|
|
return '99+';
|
|
|
|
} else {
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
}
|