Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Numbers


Share

Dart Numbers

Dart has two types of numbers:

  • int for whole numbers, like 7, -150 or 2024
  • double for numbers with decimals, like 3.5 or -0.25

Both types belong to a common type called num.


int

An int is a whole number, positive or negative, without decimals:

Example

void main() {
  int apples = 12;
  int temperature = -5;
  int population = 8000000000;
  print(apples);
  print(temperature);
  print(population);
}
Try it Yourself »

Result:

12
-5
8000000000

You cannot store a number with decimals in an int variable:

Example

void main() {
  int price = 9.99;
  print(price);
}
Try it Yourself »

Result:

main.dart:2:15: Error: A value of type 'double' can't be assigned to a variable of type 'int'.
  int price = 9.99;
              ^


double

A double is a number with decimals:

Example

void main() {
  double price = 9.99;
  double height = 1.75;
  double rate = 2;
  print(price);
  print(height);
  print(rate);
}
Try it Yourself »

Result:

9.99
1.75
2.0

Note that rate was given the whole number 2, but because it is a double, Dart prints it as 2.0.

A double can also be written in scientific notation, with an e that means "times 10 to the power of":

Example

void main() {
  double big = 1.5e3;
  double small = 2e-4;
  print(big);
  print(small);
}
Try it Yourself »

Result:

1500.0
0.0002

num

A variable of type num can hold an int or a double:

Example

void main() {
  num score = 10;
  print(score);
  score = 9.5;
  print(score);
}
Try it Yourself »

Result:

10
9.5

When you divide two numbers with /, the result is always a double.

To get a whole number result, use ~/ instead:

Example

void main() {
  print(10 / 2);
  print(7 / 2);
  print(7 ~/ 2);
}
Try it Yourself »

Result:

5.0
3.5
3

You will learn more about math operators in the Arithmetic chapter.


Number Properties and Methods

Numbers in Dart have built-in properties (values you can read) and methods (actions you can call).

You use them with a dot after the number or the variable:

Example

void main() {
  int a = -8;
  double b = 4.6;
  print(a.abs());
  print(b.round());
  print(b.floor());
  print(b.ceil());
  print(a.isEven);
  print(a.isNegative);
}
Try it Yourself »

Result:

8
5
4
5
true
true

Use toStringAsFixed() to show a number with a fixed number of decimals, for example a price:

Example

void main() {
  double total = 19.987;
  print(total.toStringAsFixed(2));
  print(total.toStringAsFixed(0));
}
Try it Yourself »

Result:

19.99
20

This table lists some common number properties and methods:

Property or methodDescriptionExampleResult
abs()Returns the number without its minus sign(-8).abs()8
round()Rounds to the nearest whole number4.6.round()5
floor()Rounds down4.6.floor()4
ceil()Rounds up4.2.ceil()5
toInt()Removes the decimals and returns an int4.9.toInt()4
toDouble()Returns the number as a double5.toDouble()5.0
toStringAsFixed(n)Returns a string with n decimals3.14159.toStringAsFixed(2)3.14
isEventrue if an int is even4.isEventrue
isOddtrue if an int is odd7.isOddtrue
isNegativetrue if the number is less than zero5.isNegativefalse

Note: To call a method on a negative number, put the number in parentheses, like (-8).abs().

Without them, Dart reads -8.abs() as -(8.abs()), which is -8.


Parse Numbers From Text

Sometimes a number is stored as text, for example when a user types it.

Use int.parse() or double.parse() to turn the text into a number:

Example

void main() {
  var a = int.parse('42');
  var b = double.parse('3.5');
  print(a + 8);
  print(b * 2);
}
Try it Yourself »

Result:

50
7.0

You will learn more in the Type Conversion chapter.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.