Percentage

Format decimal values as percentage strings with null-safe handling.

formatPercentage

Converts decimal values (0-1) into percentage strings with null-safe handling and database string support. Ideal for confidence scores, accuracy metrics, and progress indicators.

import { formatPercentage } from "@tilt-legal/cubitt-components/utilities/formatters";

formatPercentage(0.85); // "85%"
formatPercentage(0.856, { decimals: 1 }); // "85.6%"
formatPercentage(null); // "-"
formatPercentage("0.75"); // "75%"
PropertyTypeDefaultDescription
decimalsnumber0Number of decimal places
fallbackstring"-"String to return for null/invalid values

Handling null values

Gracefully handles null, undefined, and invalid inputs:

formatPercentage(null); // "-"
formatPercentage(undefined); // "-"
formatPercentage(NaN); // "-"
formatPercentage("invalid"); // "-"
formatPercentage(null, { fallback: "N/A" }); // "N/A"

Database string support

Accepts string values directly from database queries:

formatPercentage("0.85"); // "85%"
formatPercentage("0.9"); // "90%"

On this page