cGetOffer function

String cGetOffer({
  1. required double offerPrice,
  2. required double ordinalPrice,
  3. dynamic round = false,
  4. dynamic suffix = ' %',
  5. dynamic prefix = '',
})

Calculates the discount percentage based on the original price and the offer price.

Returns a formatted string representing the discount percentage with optional prefix and suffix.

  • offerPrice: The price after the discount.
  • ordinalPrice: The original price before the discount.
  • round: If true, the result is rounded to the nearest integer.
  • suffix: An optional suffix to append to the result (e.g., " %").
  • prefix: An optional prefix to prepend to the result.

Example usage:

final discount = cGetOffer(offerPrice: 45.0, ordinalPrice: 60.0, round: true); // Returns "25 %"

Implementation

String cGetOffer({
  required double offerPrice,
  required double ordinalPrice,
  round = false,
  suffix = ' %',
  prefix = '',
}) {
  var data = ((ordinalPrice - offerPrice) / ordinalPrice) * 100;
  if (round) {
    return '$prefix${(data).round()}$suffix';
  }
  return '$prefix${(data).toStringAsFixed(2).replaceAll('.00', '')}$suffix';
}