Skip to main content

Overview

The Discount Calculator is one of Numix’s core features, designed to help businesses accurately calculate discounted prices with support for multiple discount types, cascading discounts, and tax calculations.

Percentage Discounts

Calculate discounts as a percentage of the original price

Fixed Amount

Apply discounts as fixed monetary amounts

Multiple Discounts

Apply primary and additional discounts sequentially

Tax Calculation

Calculate taxes on the discounted subtotal

Discount Modes

The calculator supports two distinct discount calculation modes:

Percentage Mode

In percentage mode, discounts are applied as percentages of the current price. This mode uses cascading discount logic, where each subsequent discount is applied to the already-discounted price.
discount_provider.dart
if (_discountType == DiscountType.percentage) {
  if (primaryDiscount > 100 || additionalDiscount > 100) {
    _errorMessage = "Los porcentajes de descuento no pueden exceder 100%";
    return;
  }

  // Apply primary discount
  double saved1 = currentPrice * (primaryDiscount / 100);
  currentPrice -= saved1;
  totalSaved += saved1;

  // Apply additional discount to already-discounted price
  if (additionalDiscount > 0) {
    double saved2 = currentPrice * (additionalDiscount / 100);
    currentPrice -= saved2;
    totalSaved += saved2;
  }
}
Cascading Discount Example: A $100 item with 20% + 10% discounts:
  • After first discount: 100(100 - (100 × 0.20) = $80
  • After second discount: 80(80 - (80 × 0.10) = $72
  • Total savings: 28(not28 (not 30!)

Fixed Amount Mode

In fixed amount mode, discounts are applied as absolute monetary values. Multiple fixed discounts are simply added together.
discount_provider.dart
else {
  double totalFixedDiscount = primaryDiscount + additionalDiscount;
  if (totalFixedDiscount > origPrice) {
    _errorMessage = "El descuento no puede ser mayor al precio original";
    return;
  }
  totalSaved = totalFixedDiscount;
  currentPrice = origPrice - totalFixedDiscount;
}

Calculation Flow

The calculator follows a precise sequence:
1

Parse and Validate Input

All inputs are safely parsed using double.tryParse() to prevent crashes from invalid input.
discount_provider.dart
final origPrice = double.tryParse(_originalPriceInput);
final primaryDiscount = double.tryParse(_primaryDiscountInput);
final additionalDiscount = _additionalDiscountInput.isEmpty ? 0.0 : double.tryParse(_additionalDiscountInput);
final taxPercent = _taxInput.isEmpty ? 0.0 : double.tryParse(_taxInput);

if (origPrice == null || primaryDiscount == null || additionalDiscount == null || taxPercent == null) {
  _errorMessage = "Valores numéricos inválidos";
  return;
}
2

Apply Discount Logic

Discounts are applied according to the selected mode (percentage or fixed amount), with proper validation at each step.
3

Calculate Tax

Tax is calculated on the discounted subtotal, not the original price:
discount_provider.dart
_subtotal = currentPrice;
_taxAmount = _subtotal! * (taxPercent / 100);
_finalPrice = _subtotal! + _taxAmount!;
4

Update UI

Results are exposed through getters and the UI is notified via notifyListeners().

Validation Rules

The calculator enforces strict business rules to prevent invalid calculations:
All values (price, discounts, tax) must be non-negative.
if (origPrice < 0 || primaryDiscount < 0 || additionalDiscount < 0 || taxPercent < 0) {
  _errorMessage = "Los valores no pueden ser negativos";
  return;
}
In percentage mode, discounts cannot exceed 100%.
if (primaryDiscount > 100 || additionalDiscount > 100) {
  _errorMessage = "Los porcentajes de descuento no pueden exceder 100%";
  return;
}
In fixed amount mode, total discounts cannot exceed the original price.
if (totalFixedDiscount > origPrice) {
  _errorMessage = "El descuento no puede ser mayor al precio original";
  return;
}

Input Fields

The calculator accepts four input values:
FieldTypeRequiredDescription
Original PriceNumberYesThe starting price before any discounts
Primary DiscountNumberYesThe first discount to apply (% or fixed amount)
Additional DiscountNumberNoOptional second discount (% or fixed amount)
Tax PercentageNumberNoTax rate to apply to the discounted price

Output Values

After calculation, the following values are available:
  • Subtotal: Price after all discounts, before tax
  • Saved Amount: Total monetary value saved from discounts
  • Tax Amount: Tax calculated on the subtotal
  • Final Price: Subtotal plus tax (the amount customer pays)

State Persistence

All inputs are automatically saved to device storage using SharedPreferences:
discount_provider.dart
_prefs.setString('disc_orig', originalPriceStr);
_prefs.setString('disc_pri', primaryDiscountStr);
_prefs.setString('disc_add', additionalDiscountStr);
_prefs.setString('disc_tax', taxStr);
_prefs.setInt('disc_type', type == DiscountType.percentage ? 0 : 1);
When the app restarts, the calculator automatically restores the last used values and recalculates the results.
For implementation details and API documentation, see the Discount Provider API Reference.

Usage Example

Scenario: A store offers 20% off a $100 item, plus an additional 10% off for loyalty members, with 8% sales tax.
1

Enter Values

  • Original Price: 100
  • Primary Discount: 20 (percentage mode)
  • Additional Discount: 10
  • Tax: 8
2

Results

  • Subtotal: $72.00 (100100 → 80 → $72)
  • Saved: $28.00
  • Tax: $5.76 (8% of $72)
  • Final Price: $77.76

Sales Price Calculator

Calculate optimal pricing with markup or margin

Mathematical Precision

Learn how Numix ensures calculation accuracy

Build docs developers (and LLMs) love