Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | 320x 316x 316x 316x 316x 209x 209x 103x 103x 569x 94x 241x 241x 241x 184x 184x 80x 80x 80x 80x 80x 80x 80x 80x 10x 10x | export function formatAmount(
value: string | number | null | undefined,
{ maximumFractionDigits = 4 }: { maximumFractionDigits?: number } = {},
) {
if (value == null || value === "") return "-";
const parsed = Number(value);
Iif (!Number.isFinite(parsed)) return String(value);
const formatted = new Intl.NumberFormat("en-NZ", {
maximumFractionDigits,
}).format(Math.abs(parsed));
return parsed < 0 ? `(${formatted})` : formatted;
}
export function formatUnit(
unit: string | null | undefined,
resultCurrency: string,
) {
Iif (!unit) return "-";
return unit.replace(/^currency(?=\/|$)/, resultCurrency || "currency");
}
const INPUT_DECIMAL_PATTERN = /^([+-]?)(\d*)(\.\d*)?$/;
const CURRENCY_UNIT_PATTERN =
/(^|\/)(?:currency|NZD|USD|AUD|CAD|EUR|GBP|MNZD|MUSD|MAUD|MCAD|MEUR|MGBP)(?=\/|$)/i;
export function stripNumericInputFormatting(value: string) {
return value.replace(/,/g, "");
}
export function unitLooksMonetary(unit: string | null | undefined) {
return CURRENCY_UNIT_PATTERN.test((unit ?? "").trim());
}
export function formatNumericInputValue(
value: string | number | null | undefined,
{
defaultToZero = true,
useGrouping = false,
trimTrailingZeros = false,
}: {
defaultToZero?: boolean;
useGrouping?: boolean;
trimTrailingZeros?: boolean;
} = {},
) {
const rawValue = value == null ? "" : String(value);
const normalizedValue = stripNumericInputFormatting(rawValue);
if (!normalizedValue.trim()) return defaultToZero ? "0" : "";
const displayValue = trimTrailingZeros
? trimDecimalTrailingZeros(normalizedValue)
: normalizedValue;
if (!useGrouping) return displayValue;
const match = displayValue.match(INPUT_DECIMAL_PATTERN);
Iif (!match) return displayValue;
const [, sign, wholePart, fractionPart] = match;
Iif (!wholePart) return displayValue;
const groupedWhole = groupWholeNumber(wholePart);
return `${sign}${groupedWhole}${fractionPart ?? ""}`;
}
function groupWholeNumber(value: string) {
const normalized = value.replace(/^0+(?=\d)/, "");
return normalized.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function trimDecimalTrailingZeros(value: string) {
Iif (!INPUT_DECIMAL_PATTERN.test(value)) return value;
return value.replace(/(\.\d*?)0+$/, "$1").replace(/\.$/, "");
}
|