Dart num floor()
Syntax & Examples
num.floor() method
The `floor` method in Dart returns the greatest integer no greater than the given number.
Syntax of num.floor()
The syntax of num.floor() method is:
int floor()
This floor() method of num returns the greatest integer no greater than this.
Return Type
num.floor() returns value of type int
.
✐ Examples
1 Floor of a decimal number
In this example,
- We create a num variable
num1
with the value 4.8. - We use the
floor()
method to get its floor value as an integer. - We then print the result to standard output.
Dart Program
void main() {
num num1 = 4.8;
int floorValue = num1.floor();
print('Floor of $num1: $floorValue');
}
Output
Floor of 4.8: 4
2 Floor of a negative decimal number
In this example,
- We create a num variable
negativeNum
with the value -3.2. - We use the
floor()
method to get its floor value as an integer. - We then print the result to standard output.
Dart Program
void main() {
num negativeNum = -3.2;
int floorValue = negativeNum.floor();
print('Floor of $negativeNum: $floorValue');
}
Output
Floor of -3.2: -4
3 Floor of an integer
In this example,
- We create a num variable
integerNum
with the value 7.0, which is already an integer. - We use the
floor()
method to get its floor value as an integer. - We then print the result to standard output.
Dart Program
void main() {
num integerNum = 7.0;
int floorValue = integerNum.floor();
print('Floor of $integerNum: $floorValue');
}
Output
Floor of 7: 7
Summary
In this Dart tutorial, we learned about floor() method of num: the syntax and few working examples with output and detailed explanation for each example.