Dart double floor()
Syntax & Examples


double.floor() method

The `floor` method in Dart returns the greatest integer that is not greater than the given number.


Syntax of double.floor()

The syntax of double.floor() method is:

 int floor() 

This floor() method of double returns the greatest integer no greater than this.

Return Type

double.floor() returns value of type int.



✐ Examples

1 Floor of a decimal number

In this example,

  1. We create a double variable num with the value 4.8.
  2. We use the floor() method to get its floor value as an integer.
  3. We then print the result to standard output.

Dart Program

void main() {
  double num = 4.8;
  int floorNum = num.floor();
  print('Floor of $num: $floorNum');
}

Output

Floor of 4.8: 4

2 Floor of a negative decimal number

In this example,

  1. We create a double variable negativeNum with the value -3.2.
  2. We use the floor() method to get its floor value as an integer.
  3. We then print the result to standard output.

Dart Program

void main() {
  double negativeNum = -3.2;
  int floorNegative = negativeNum.floor();
  print('Floor of $negativeNum: $floorNegative');
}

Output

Floor of -3.2: -4

3 Floor of an integer

In this example,

  1. We create a double variable integerNum with the value 7.0, which is already an integer.
  2. We use the floor() method to get its floor value as an integer.
  3. We then print the result to standard output.

Dart Program

void main() {
  double integerNum = 7.0;
  int floorInteger = integerNum.floor();
  print('Floor of $integerNum: $floorInteger');
}

Output

Floor of 7: 7

Summary

In this Dart tutorial, we learned about floor() method of double: the syntax and few working examples with output and detailed explanation for each example.