formatNumber method

String formatNumber(
  1. num value
)

Format a numeric value according to these rules

Implementation

String formatNumber(num value) {
  // Round to specified decimal points
  final rounded = value.toStringAsFixed(decimalPoint);

  // Remove trailing zeros from decimal part
  final cleanedNumber = _removeTrailingZeros(rounded);

  // Add comma separation
  if (commaSperated) {
    final parts = cleanedNumber.split('.');
    final intPart = parts[0];
    final decimalPart = parts.length > 1 ? '.${parts[1]}' : '';

    // Add commas every 3 digits
    final buffer = StringBuffer();
    for (int i = 0; i < intPart.length; i++) {
      if (i > 0 && (intPart.length - i) % 3 == 0) {
        buffer.write(',');
      }
      buffer.write(intPart[i]);
    }

    final formatted = buffer.toString() + decimalPart;
    return currency ? '₹$formatted' : formatted;
  }

  return currency ? '₹$cleanedNumber' : cleanedNumber;
}