formatWithMax method

String formatWithMax({
  1. required num max,
  2. String? prefix,
  3. String? suffix,
  4. int decimalPlaces = 0,
  5. String customFormat(
    1. num value
    )?,
})

Formats the number by adding a prefix or suffix based on its value compared to a threshold, with optional formatting for decimals.

Implementation

String formatWithMax({
  required num max,
  String? prefix,
  String? suffix,
  int decimalPlaces = 0,
  String Function(num value)? customFormat,
}) {
  if (isNull()) {
    return '';
  }
  // Apply a custom format if provided
  if (customFormat != null) {
    return customFormat(this ?? 0);
  }

  // Round the number to the specified decimal places
  final roundedValue = decimalPlaces > 0
      ? (this ?? 0.0).toStringAsFixed(decimalPlaces)
      : toString();

  // Add prefix and suffix if the number exceeds the threshold
  if ((this ?? 0) > max) {
    return '${prefix ?? ''}$roundedValue${suffix ?? ''}';
  }

  return roundedValue; // Return the formatted number as a string
}