The `floor` method in Dart returns the greatest integer that is not greater than the given number.
The syntax of double.floor() method is:
int floor()
This floor() method of double returns the greatest integer no greater than this
.
double.floor() returns value of type int
.
In this example,
num
with the value 4.8.floor()
method to get its floor value as an integer.void main() {
double num = 4.8;
int floorNum = num.floor();
print('Floor of $num: $floorNum');
}
Floor of 4.8: 4
In this example,
negativeNum
with the value -3.2.floor()
method to get its floor value as an integer.void main() {
double negativeNum = -3.2;
int floorNegative = negativeNum.floor();
print('Floor of $negativeNum: $floorNegative');
}
Floor of -3.2: -4
In this example,
integerNum
with the value 7.0, which is already an integer.floor()
method to get its floor value as an integer.void main() {
double integerNum = 7.0;
int floorInteger = integerNum.floor();
print('Floor of $integerNum: $floorInteger');
}
Floor of 7: 7
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.