feat: make brokerage account list compact (#111)
This commit is contained in:
@@ -2,6 +2,7 @@ const DECIMAL_PART_COUNT = 2;
|
||||
const CENTS_PRECISION = 2;
|
||||
|
||||
type FormatMoneyOptions = {
|
||||
withRoundedCents?: boolean;
|
||||
withZeroCents?: boolean;
|
||||
};
|
||||
|
||||
@@ -39,16 +40,20 @@ function formatMoney(
|
||||
money: Money
|
||||
| null
|
||||
| undefined,
|
||||
{ withZeroCents = false }: FormatMoneyOptions = {},
|
||||
{ withRoundedCents = false, withZeroCents = false }: FormatMoneyOptions = {},
|
||||
): string {
|
||||
if (!money) return "—";
|
||||
|
||||
const [integerPart = "", fractionPart = ""] = money.value.split(
|
||||
const value = withRoundedCents
|
||||
? roundDecimalToCents(money.value)
|
||||
: money.value;
|
||||
|
||||
const [integerPart = "", fractionPart = ""] = value.split(
|
||||
".",
|
||||
DECIMAL_PART_COUNT,
|
||||
);
|
||||
|
||||
const normalizedFraction = withZeroCents
|
||||
const normalizedFraction = withRoundedCents || withZeroCents
|
||||
? fractionPart.padEnd(CENTS_PRECISION, "0")
|
||||
: fractionPart;
|
||||
|
||||
@@ -61,6 +66,23 @@ function formatMoney(
|
||||
return `${formatDecimal(decimal)} ${currency}`;
|
||||
}
|
||||
|
||||
function roundDecimalToCents(value: string): string {
|
||||
const [integer = "", fraction = ""] = value.split(".", DECIMAL_PART_COUNT);
|
||||
const sign = integer.startsWith("-") ? "-" : "";
|
||||
const digits = sign ? integer.slice(1) : integer;
|
||||
const roundedFraction = fraction.padEnd(CENTS_PRECISION + 1, "0");
|
||||
const cents = `${digits}${roundedFraction.slice(0, CENTS_PRECISION)}`;
|
||||
const roundingDigit = roundedFraction.at(CENTS_PRECISION) ?? "0";
|
||||
|
||||
const rounded = roundingDigit >= "5"
|
||||
? (BigInt(cents) + 1n).toString().padStart(cents.length, "0")
|
||||
: cents;
|
||||
|
||||
const roundedInteger = rounded.slice(0, -CENTS_PRECISION) || "0";
|
||||
|
||||
return `${sign}${roundedInteger}.${rounded.slice(-CENTS_PRECISION)}`;
|
||||
}
|
||||
|
||||
// Без timeZone Intl берёт часовой пояс браузера пользователя.
|
||||
const snapshotFormatter = new Intl.DateTimeFormat("ru-RU", {
|
||||
dateStyle: "short",
|
||||
|
||||
Reference in New Issue
Block a user