Skip to content

js: float/compact_number displayers have no prefix/suffix — can't render pre-scaled money columns as $34.4M #960

Description

@paddymul

Problem

getFloatFormatter and getCompactNumberFormatter in Displayer.ts build their Intl.NumberFormat from a fixed option set — min_fraction_digits/max_fraction_digits for float, a hardcoded notation: 'compact', maximumFractionDigits: 1 for compact_number — with no prefix or suffix (Displayer.ts:130-150, Displayer.ts:166-173). FloatDisplayerA and the rest of DisplayerArgs (DFWhole.ts) carry no prefix/suffix field either. No displayer composes a literal string around a formatted number.

Impact

A project styling a money column has no way to render $34.4M while keeping the underlying cell value numeric for correct grid sorting. Workarounds are both lossy: compact_number on raw-dollar-scale data gets the M suffix but never $, and converting the column to a string loses numeric sort order (AG Grid sorts on raw cell value, and a string column's raw value is the formatted text).

Suggested fix

Don't bolt prefix/suffix onto float and compact_number individually — that duplicates the same wrapping logic per numeric displayer and leaves integer out. Instead make it generic: add optional prefix/suffix string fields to the shared numeric displayer args (or to FormatterArgs itself), and apply them once in getFormatter() by wrapping whichever formatter function the existing switch already returns:

export function getFormatter(fArgs: FormatterArgs): ValueFormatterFunc<unknown> {
    const base = getBaseFormatter(fArgs); // current switch(fArgs.displayer) body, unchanged
    const { prefix, suffix } = fArgs as { prefix?: string; suffix?: string };
    if (!prefix && !suffix) return base;
    return (params) => {
        const formatted = base(params);
        return formatted === "" ? formatted : `${prefix ?? ""}${formatted}${suffix ?? ""}`;
    };
}

That gets float, integer, and compact_number all covered in one place, is backward compatible (both default to ''/undefined), and any future numeric displayer inherits it for free.

Context

Hit while styling contract-value columns in a new tallyman project (NFL salary data, values pre-scaled to millions). Worked around for now with a derived raw-dollar column plus the existing compact_number displayer (no $).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions