Groovy Math.floor syntax
Math.floor(x)
In Groovy, Math.floor rounds toward negative infinity. The floor of 2.3 is 2, the floor of −2.3 is −3, and 2 stays 2.
Math.floor
println(Math.floor(2.8d))
2.0
Math.floor(x)
| Input | A double, or a number convertible to double, for the java.lang.Math method. |
|---|---|
| Result | A double. Infinities and NaN pass through unchanged. |
A test bench increases a reading by one tenth of a unit at a time. Watch where the rounded display changes as the sample passes from one integer toward the next.
import java.lang.Math;
class MathExample {
public static void main(String[] args) {
println("The floor value of 1.0 is " + Math.floor(1.0d));
println("The floor value of 1.1 is " + Math.floor(1.1d));
println("The floor value of 1.2 is " + Math.floor(1.2d));
println("The floor value of 1.3 is " + Math.floor(1.3d));
println("The floor value of 1.4 is " + Math.floor(1.4d));
println("The floor value of 1.5 is " + Math.floor(1.5d));
println("The floor value of 1.6 is " + Math.floor(1.6d));
println("The floor value of 1.7 is " + Math.floor(1.7d));
println("The floor value of 1.8 is " + Math.floor(1.8d));
println("The floor value of 1.9 is " + Math.floor(1.9d));
println("The floor value of 2.0 is " + Math.floor(2.0d));
}
}
The floor value of 1.0 is 1.0 The floor value of 1.1 is 1.0 The floor value of 1.2 is 1.0 The floor value of 1.3 is 1.0 The floor value of 1.4 is 1.0 The floor value of 1.5 is 1.0 The floor value of 1.6 is 1.0 The floor value of 1.7 is 1.0 The floor value of 1.8 is 1.0 The floor value of 1.9 is 1.0 The floor value of 2.0 is 2.0
A scheduling display groups positive measurements into whole-unit intervals. Compare readings just on either side of an integer to see which interval the display would select.
import java.lang.Math;
class MathExample {
public static void main(String[] args) {
println("The floor value of 1.5698 is " + Math.floor(1.5698d));
println("The floor value of 1.3093 is " + Math.floor(1.3093d));
println("The floor value of 1.0087 is " + Math.floor(1.0087d));
println("The floor value of 1.1038 is " + Math.floor(1.1038d));
println("The floor value of 1.9573 is " + Math.floor(1.9573d));
println("The floor value of 1.7462 is " + Math.floor(1.7462d));
println("The floor value of 1.6427 is " + Math.floor(1.6427d));
println("The floor value of 1.4128 is " + Math.floor(1.4128d));
println("The floor value of 1.8099 is " + Math.floor(1.8099d));
println("The floor value of 1.2743 is " + Math.floor(1.2743d));
println("The floor value of 2.0001 is " + Math.floor(2.0001d));
}
}
The floor value of 1.5698 is 1.0 The floor value of 1.3093 is 1.0 The floor value of 1.0087 is 1.0 The floor value of 1.1038 is 1.0 The floor value of 1.9573 is 1.0 The floor value of 1.7462 is 1.0 The floor value of 1.6427 is 1.0 The floor value of 1.4128 is 1.0 The floor value of 1.8099 is 1.0 The floor value of 1.2743 is 1.0 The floor value of 2.0001 is 2.0
A temperature logger records values just below zero or another integer boundary. Follow the rounding direction on the negative side before using the result as the label of a chart interval.
import java.lang.Math;
class MathExample {
public static void main(String[] args) {
println("The floor value of -1.5698 is " + Math.floor(-1.5698d));
println("The floor value of -1.3093 is " + Math.floor(-1.3093d));
println("The floor value of -1.0087 is " + Math.floor(-1.0087d));
println("The floor value of -1.1038 is " + Math.floor(-1.1038d));
println("The floor value of -1.9573 is " + Math.floor(-1.9573d));
println("The floor value of -1.7462 is " + Math.floor(-1.7462d));
println("The floor value of -1.6427 is " + Math.floor(-1.6427d));
println("The floor value of -1.4128 is " + Math.floor(-1.4128d));
println("The floor value of -1.8099 is " + Math.floor(-1.8099d));
println("The floor value of -1.2743 is " + Math.floor(-1.2743d));
println("The floor value of -2.0001 is " + Math.floor(-2.0001d));
}
}
The floor value of -1.5698 is -2.0 The floor value of -1.3093 is -2.0 The floor value of -1.0087 is -2.0 The floor value of -1.1038 is -2.0 The floor value of -1.9573 is -2.0 The floor value of -1.7462 is -2.0 The floor value of -1.6427 is -2.0 The floor value of -1.4128 is -2.0 The floor value of -1.8099 is -2.0 The floor value of -1.2743 is -2.0 The floor value of -2.0001 is -3.0
A sensor can use infinity to mark a reading outside its measuring range. Pass both signs through this operation so the display can handle the result or the reported error deliberately.
import java.lang.Math;
class MathExample {
public static void main(String[] args) {
println("The floor value of Infinity is " + Math.floor(Double.POSITIVE_INFINITY));
println("The floor value of -Infinity is " + Math.floor(Double.NEGATIVE_INFINITY));
}
}
The floor value of Infinity is Infinity The floor value of -Infinity is -Infinity
The costume room has 8.5 metres of ribbon left before a performance. Each bag needs one unbroken 1.2-metre strap. The volunteer needs the number of bags she can finish from this roll.
def metres = 8.5
def perStrap = 1.2
def straps = ((int) Math.floor(metres / perStrap))
println((straps).toString() + " straps")
7 straps
Seven straps use 8.4 metres. The last tenth of a metre cannot make another strap.
Reference: Java Math API used in this example