DartBasic 2 min read

Dart operators and numbers: calculate a shopping total

Use integer minor units, arithmetic, comparisons and boolean rules for a small order.

On this page

A checkout calculation combines values and decisions. Keep money in integer minor units in this beginner example so a display calculation does not accidentally depend on binary floating-point rounding. A real payment integration must also follow its currency’s precision rules.

Worked example

Language requirement: Dart 3.0 or later. This complete example uses browser-supported libraries.

main.dart
void main() {
  const pricePaise = 24900;
  const quantity = 3;
  final subtotal = pricePaise * quantity;
  final eligible = subtotal >= 50000 && quantity > 0;
  final discount = eligible ? subtotal ~/ 10 : 0;
  final total = subtotal - discount;
  print('Subtotal: $subtotal paise');
  print('Discount: $discount paise');
  print('Total: $total paise');
  print('Whole rupees: ${total ~/ 100}; remainder: ${total % 100}');
}

Follow the data and control flow

Multiplication creates a subtotal of 74700 paise. The boolean expression requires both the threshold and a positive quantity, so the discount is 7470 and the final total is 67230. The conditional expression chooses a value; it does not mutate the original price.

/ produces a fractional result, while ~/ performs truncating integer division and % returns the remainder. Parentheses make a financial rule easier to review even when operator precedence would produce the same answer. Keep business rounding rules explicit rather than assuming truncation is always the correct policy.

A mistake to avoid

An int can still contain a negative or unreasonable quantity. Types identify supported operations, not every domain constraint. Reject invalid order input before charging. Avoid comparing doubles for exact financial equality and do not use assert as production input validation.

Practice and verification

Try quantities 0, 1 and 3 and predict eligibility before running. Add a delivery fee only below the free-delivery threshold. Write down expected totals in paise and verify the boundary exactly at the threshold.

How this topic connects

Understand first

Apply it next

Lesson progressMark complete after checking the example and exercise.

Find your next idea

Search lessons, code examples and projects.

Type a few words to start exploring.