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 Math


Share

Dart Math

You can do simple math with the arithmetic operators, like +, -, * and /.

For more math, Dart has the dart:math library.

To use it, import it at the top of your file:

import 'dart:math';

max() and min()

The max() function returns the larger of two numbers.

The min() function returns the smaller of two numbers:

Example

import 'dart:math';

void main() {
  print(max(5, 10));
  print(min(5, 10));
  print(max(2.5, 1.5));
}
Try it Yourself »

Result:

10
5
2.5

To find the largest number in a list, you can use reduce() with max:

Example

import 'dart:math';

void main() {
  List<int> scores = [72, 95, 64, 88];
  print(scores.reduce(max));
  print(scores.reduce(min));
}
Try it Yourself »

Result:

95
64


sqrt() and pow()

The sqrt() function returns the square root of a number.

The pow(x, y) function returns x to the power of y:

Example

import 'dart:math';

void main() {
  print(sqrt(64));
  print(pow(2, 3));
  print(pow(2.0, 3));
}
Try it Yourself »

Result:

8.0
8
8.0

sqrt() always returns a double.

pow() returns an int when both numbers are integers and y is not negative.

In all other cases, it returns a double.

The library also has other functions, like sin(), cos(), tan() and log().


Math Constants

The dart:math library has some well-known numbers as constants:

Example

import 'dart:math';

void main() {
  print(pi);
  print(e);
  print(sqrt2);
}
Try it Yourself »

Result:

3.141592653589793
2.718281828459045
1.4142135623730951

This example uses pi to find the area of a circle:

Example

import 'dart:math';

void main() {
  double radius = 3;
  double area = pi * pow(radius, 2);
  print('Area: ${area.toStringAsFixed(2)}');
}
Try it Yourself »

Result:

Area: 28.27

Rounding and Absolute Value

Some math methods are built into the numbers themselves, so they do not need an import:

MethodDescription
abs()Returns the absolute (positive) value
round()Rounds to the nearest integer
ceil()Rounds up
floor()Rounds down

Example

void main() {
  print((-7).abs());
  print(4.6.round());
  print(4.2.ceil());
  print(4.8.floor());
}
Try it Yourself »

Result:

7
5
5
4

Random Numbers

The Random class makes random numbers.

  • nextInt(max) returns a random integer from 0 up to, but not including, max.
  • nextDouble() returns a random double from 0.0 up to, but not including, 1.0.
  • nextBool() returns true or false.

If you give Random() a number, called a seed, you get the same random numbers every time you run the program.

This is useful for testing, and it is why the output below never changes:

Example

import 'dart:math';

void main() {
  final random = Random(42);
  print(random.nextInt(10));
  print(random.nextInt(10));
  print(random.nextBool());
}
Try it Yourself »

Result:

7
8
true

Without a seed, Random() gives different numbers each time.

Note: The same seed can give other numbers in another version of Dart, or when Dart runs in a web browser.

To get a number in another range, add to the result.

This example rolls a dice five times, so each number is from 1 to 6:

Example

import 'dart:math';

void main() {
  final random = Random(7);
  for (int i = 0; i < 5; i++) {
    int dice = random.nextInt(6) + 1;
    print('You rolled $dice');
  }
}
Try it Yourself »

Result:

You rolled 5
You rolled 6
You rolled 3
You rolled 4
You rolled 1

Note: For passwords and other secret values, use Random.secure() instead, because its numbers are much harder to guess.



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.