computeSavingsPercent function

int? computeSavingsPercent({
  1. required double fromPricePerMonth,
  2. required double toPricePerMonth,
})

Compute the savings percentage when upgrading from from to to.

Returns a positive integer (e.g. 40 for "save 40%") when to is cheaper on a per-period basis, or null when no meaningful savings can be computed (mismatched periods, custom periods, etc.).

Implementation

int? computeSavingsPercent({
  required double fromPricePerMonth,
  required double toPricePerMonth,
}) {
  if (fromPricePerMonth <= 0 || toPricePerMonth <= 0) return null;
  if (toPricePerMonth >= fromPricePerMonth) return null;
  final pct =
      ((fromPricePerMonth - toPricePerMonth) / fromPricePerMonth) * 100;
  return pct.round();
}