What is min() in Dart?

The min() function returns the smallest value among two numbers sent as parameters. The image below shows a visual representation of the min() function.

Visual representation of the min() function

To use the min() function, you will need to import the dart:math library as shown below:

import dart:math

Syntax

The syntax of the min function is as follows:

type min(type num-1, type num-2)
// Type extends number

Parameters

The min() function takes two numbers as parameters.

The numbers can be Int, Double, Float, or Long.

Return value

The min() function returns the smallest value from the two numbers sent as parameters.

Code

The code below shows how to use min() in Dart.

import 'dart:convert';
import 'dart:math';
void main() {
// two positive numbers
print("The value of min(10, 0) = ${min(10, 0)}");
//one positive and the other negative
print("The value of min(4, -6) = ${min(4, -6)}");
// both negative numbers
print("The value of min(-10, -9) = ${min(-10, -9)}");
// both double numbers
print("The value of min(12.234,1.2345) = ${min(12.234,1.2345)}");
// one int and the other double
print("The value of min(12.234,1) = ${min(12.234,1)}");
}

For every pair of values in the code above, the min() function returns the smaller of the two.

Free Resources