arm-core/web/src/widgets/ConsolidatedOsv/useNumberDisplay.tsx
Andrey Epifancev 54de39727b fix(web): «Единицы» сводной ОСВ — масштаб на строках без float, ноль — «—» (AURORA-1589, PR #166)
formatScaledAmount в utils/money.ts: запятая сдвигается на 3/6/9 разрядов в строке,
округление половины от нуля на BigInt; нулевая сумма — «—» (zeroDash, как до 71ef633).
Подписи «млн руб.», «млрд руб.» без точки. Исправляет коммит 71ef633.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 01:15:55 +04:00

30 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useCallback, useState } from 'react';
import { formatScaledAmount } from '../../utils/money';
export type Unit = 'руб.' | 'тыс. руб.' | 'млн руб.' | 'млрд руб.';
/** Единицы показа сумм сводной ОСВ и степень десяти (суммы API — в рублях). */
export const UNIT_SCALE: Record<Unit, number> = {
'руб.': 0,
'тыс. руб.': 3,
'млн руб.': 6,
'млрд руб.': 9,
};
export const UNITS: { value: Unit; label: string }[] = (Object.keys(UNIT_SCALE) as Unit[]).map((u) => ({ value: u, label: u }));
export const PRECISIONS = [0, 1, 2, 3] as const;
/** «Единицы» и «Знаков после запятой» сводной ОСВ [30]: только показ, суммы считаются на строках (`utils/money.ts`). */
export function useNumberDisplay(initial?: { unit?: Unit; precision?: number }) {
const [unit, setUnit] = useState<Unit>(initial?.unit ?? 'руб.');
const [precision, setPrecision] = useState(initial?.precision ?? 2);
const format = useCallback(
(raw: string | null) => formatScaledAmount(raw, { scale: UNIT_SCALE[unit], precision, zeroDash: true }),
[unit, precision],
);
return { unit, setUnit, precision, setPrecision, format };
}