toPercentage method

String toPercentage({
  1. int fractionDigits = 2,
})

Converts the num to a percentage string. If the value is greater than 1, it is capped at 100%.

Example:

print(0.1234.toPercentage()); // 12.34%
print(1.2.toPercentage()); // 100.00%

Implementation

String toPercentage({int fractionDigits = 2}) {
  // logic: cap at 1 if greater than 1, otherwise use original value
  final constrained = this > 1 ? 1 : this;

  // convert to percentage: multiply by 100
  final percentage = constrained * 100;

  // format output
  return "${percentage.toStringAsFixed(fractionDigits)}%";
}