All lessons

Numbers & Math

6 min · Dart

Do math with variables and the basic operators.

Dart · Numbers & Math0/5 cleared

Enemy 1 of 5

Arithmetic and Dart's number types

This multiplies price by quantity first, then print() shows the result. Remember that / gives a decimal and ~/ gives a whole number. Press Run to see it.

Why this works

Dart supports the familiar arithmetic operators: + (add), - (subtract), * (multiply), and / (divide), usable on numeric literals or on variables that hold numbers, and evaluated with the standard order of operations: multiplication and division before addition and subtraction, unless parentheses say otherwise.

Dart has two numeric types worth knowing early: int for whole numbers and double for decimals. Regular division with / always produces a double, even when dividing two ints evenly (10 / 2 gives 5.0, not 5), if you specifically want integer (whole-number, truncated) division, Dart provides a separate operator, ~/, e.g. 10 ~/ 3 gives 3. This is a genuine point of confusion for people coming from languages where / on two integers gives you an integer result.

In the example, price * quantity is computed first, following order of operations, and only the resulting value is handed to print() to display.

You
Syntax Skeleton
Loading editor…
Sign up to fightThis one runs on our servers, so it needs a free account.