0Pricing
Dart Academy · Lesson

int vs double and Integer Division

Mix number types and use the tilde-slash operator.

int vs double and Integer Division is a free Dart Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Two Number Types

Dart has two number types: int for whole numbers and double for decimals. Both descend from num, so they mix in math. 🔢

void main() {
  int age = 30;
  double price = 4.99;
}

int Holds Whole Numbers

An int stores values with no fractional part, like counts and indexes. Writing 5 with no dot makes it an int automatically.

void main() {
  int count = 5;
  print(count); // 5
}

double Holds Decimals

A double stores floating-point numbers. The decimal point is the clue: 5.0 is a double even though it looks like a whole value.

void main() {
  double pi = 3.14;
  print(pi); // 3.14
}

Mixing Promotes to double

When an int meets a double in math, the result becomes a double. Dart widens the int so no precision is lost.

void main() {
  print(3 + 2.0); // 5.0
}

Slash Always Returns double

The plain / operator always produces a double, even for ints. So 9 / 3 gives 3.0, ready for decimal math.

void main() {
  print(9 / 3); // 3.0
}

Integer Division With Tilde-Slash

Use ~/ for integer division. It divides and throws away the remainder, returning a clean int instead of a double.

void main() {
  print(17 ~/ 5); // 3
}

Pairing ~/ With %

Together ~/ and % split a number into a quotient and a remainder. Great for converting totals into rows and leftovers.

void main() {
  print(17 ~/ 5); // 3
  print(17 % 5);  // 2
}

Converting double to int

Call toInt() on a double to drop the fraction toward zero. It truncates rather than rounds, so 4.9 becomes 4.

void main() {
  print(4.9.toInt()); // 4
}

Rounding Options

For real rounding use round(), or floor() and ceil() to push down or up. Pick the one that matches your intent.

void main() {
  print(4.5.round()); // 5
}

Converting int to double

Go the other way with toDouble(). It adds a decimal part, turning a counting number into one ready for fractional math.

void main() {
  print(7.toDouble()); // 7.0
}

Pick the Right Type

Use int when fractions make no sense, like ages or item counts. Reach for double for money, measurements, and averages.

Quick Check

Let us see which operator keeps the result a whole number.

Recap

int is whole, double is decimal, and mixing them yields a double. Use ~/ for integer division and convert with toInt and toDouble. 🎯

Frequently asked questions

Is the “int vs double and Integer Division” lesson free?

Yes — the full text of “int vs double and Integer Division” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “int vs double and Integer Division”?

Mix number types and use the tilde-slash operator. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Dart Academy?

No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “int vs double and Integer Division” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Dart Academy lesson?

Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arithmetic and Operator Precedence
  2. int vs double and Integer Division
  3. Parsing and Converting Numbers
  4. The dart:math Toolkit
← Back to Dart Academy