Skip to main content

Function: safeParsePercentage()

safeParsePercentage(value: unknown, defaultValue: number): number

Defined in: shared/utils/safeConversions.ts:159

Safely converts a value to a percentage (0-100) with fallback.

Parametersโ€‹

valueโ€‹

unknown

Value to convert to percentage

defaultValueโ€‹

number = 0

Fallback value if conversion fails (default: 0)

Returnsโ€‹

number

Valid percentage clamped between 0 and 100, or the default value

Remarksโ€‹

First converts the value to a float, then clamps the result to the valid percentage range of 0-100. Useful for progress indicators and completion ratios.

Exampleโ€‹

safeParsePercentage("75"); // 75
safeParsePercentage("150"); // 100 (clamped)
safeParsePercentage("-10"); // 0 (clamped)
safeParsePercentage("invalid"); // 0