Converts a value to its percentage of a total, rounding with precision safety.
Automatically corrects for floating-point rounding errors and returns 0 when total is 0 or invalid.
The part value.
The total value.
Number of decimal places (default: 2).
The percentage as a number.
toPercentage(50, 200); // 25toPercentage(1, 3, 3); // 33.333toPercentage(0.1 + 0.2, 0.3); // 100toPercentage(5, 0); // 0 Copy
toPercentage(50, 200); // 25toPercentage(1, 3, 3); // 33.333toPercentage(0.1 + 0.2, 0.3); // 100toPercentage(5, 0); // 0
1.2.0
Converts a value to its percentage of a total, rounding with precision safety.
Automatically corrects for floating-point rounding errors and returns 0 when total is 0 or invalid.