extension PriceExtension on num {
  /// Formats the number with commas every 3 digits
  String get formattedPrice {
    final numberString = toString();
    final parts = numberString.split('.');
    final integerPart = parts[0];
    final decimalPart = parts.length > 1 ? '.${parts[1]}' : '';

    final formattedInteger = _addCommas(integerPart);
    return formattedInteger + decimalPart;
  }

  String _addCommas(String number) {
    if (number.length <= 3) {
      return number;
    }

    final reversed = number.split('').reversed.join('');
    final chunks = <String>[];

    for (int i = 0; i < reversed.length; i += 3) {
      final end = (i + 3 < reversed.length) ? i + 3 : reversed.length;
      chunks.add(reversed.substring(i, end));
    }

    return chunks.join(',').split('').reversed.join('');
  }
}
