The max()
function returns the largest value of two numbers sent as parameters.
The figure below shows a visual representation of the max()
function.
To use the max()
function, you will need to import the dart:math
library as shown below:
import dart:math
The syntax of the max()
function is as follows:
type max(num-1, type num-2)
// type extends number
The max()
function takes two numbers as parameters.
The numbers can be of type
Int
,Double
,Float
, orLong
.
The max()
function returns the largest value from the two numbers sent as parameters.
The code below shows how to use max()
in Dart.
import 'dart:convert';import 'dart:math';void main() {// two positive numbersprint("The value of max(10, 0) = ${max(10, 0)}");//one positive and the other negativeprint("The value of max(4, -6) = ${max(4, -6)}");// both negative numbersprint("The value of max(-10, -9) = ${max(-10, -9)}");// both double numbersprint("The value of max(12.234,1.2345) = ${max(12.234,1.2345)}");// one int and the other doubleprint("The value of max(12.234,1) = ${max(12.234,1)}");}
For every pair of values in the code above, the max()
function returns the larger of the two.