withDiscount method

int withDiscount(
  1. double? discount
)

Applies a discount to the current integer value and returns the discounted amount.

The discount parameter is a nullable double representing the discount percentage (e.g., 0.2 for 20%).

If discount is null or less than or equal to 0, the original value is returned.

Returns an integer representing the value after applying the discount.

Example:

int price = 100;
double discount = 0.2; // 20% discount
int discountedPrice = price.withDiscount(discount);
print(discountedPrice); // Output: 80

int noDiscountPrice = price.withDiscount(null);
print(noDiscountPrice); // Output: 100

Implementation

int withDiscount(double? discount) {
  if (discount != null && discount > 0) {
    return (this - (this * discount)).toInt();
  }

  return this;
}