Format a numeric ratio as a percentage string.
Multiply by 100. Falsy precision uses Math.floor, including for negative ratios; truthy precision uses native toFixed and preserves trailing zeros. Floating-point arithmetic and native precision conversion remain unchanged.
Math.floor
toFixed
Usage:
import { formatPercentage } from "mazey";const ret1 = formatPercentage(0.2);const ret2 = formatPercentage(0.2, 2);console.log(ret1);console.log(ret2); Copy
import { formatPercentage } from "mazey";const ret1 = formatPercentage(0.2);const ret2 = formatPercentage(0.2, 2);console.log(ret1);console.log(ret2);
Output:
20% 20.00% Copy
20% 20.00%
Floating-point ratio to convert.
Number of decimal places, defaulting to 0.
The percentage string.
If native toFixed rejects a truthy precision value.
Format a numeric ratio as a percentage string.
Multiply by 100. Falsy precision uses
Math.floor, including for negative ratios; truthy precision uses nativetoFixedand preserves trailing zeros. Floating-point arithmetic and native precision conversion remain unchanged.Usage:
Output: