formatPercentageForClipboard function

String formatPercentageForClipboard(
  1. num? value, {
  2. int maxFractionDigits = 2,
})

Formats a numeric value into a percentage string suitable for clipboard operations.

This function takes a num value (which could be null) and returns it as a formatted string appended with a percentage sign. The formatting applied is based on the formatNumberForClipboard function, which is presumed to be defined elsewhere within the scope of this application.

Example usage:

var percentage = formatPercentageForClipboard(0.45);
print(percentage); // Output: '45%'

value is the number to format. Accepts a nullable num. If null, returns '0' to signify zero. This ensures output validity.

maxFractionDigits is an optional parameter for non-integer numbers. It sets the maximum decimal places, defaulting to 2. It's ignored for integers.

  • Returns: A String representing the formatted percentage. If the value is null, a default string '0%' is returned to represent a zero value.

Implementation

String formatPercentageForClipboard(num? value, {int maxFractionDigits = 2}) {
  final rate = (value ?? 0) * 100;
  final formattedRate = formatNumberForClipboard(rate);

  return '$formattedRate%';
}