Golang math.Floor syntax
func Floor(x float64) float64
In Go, 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
package main
import "fmt"
import "math"
func main() {
fmt.Println(math.Floor(2.8))
}
2
func Floor(x float64) float64
| Input | A float64. Import the math package. |
|---|---|
| Result | A float64. 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.
package main
import (
"fmt"
"math"
)
func main() {
var num1 = math.Floor(1.0)
var num2 = math.Floor(1.1)
var num3 = math.Floor(1.2)
var num4 = math.Floor(1.3)
var num5 = math.Floor(1.4)
var num6 = math.Floor(1.5)
var num7 = math.Floor(1.6)
var num8 = math.Floor(1.7)
var num9 = math.Floor(1.8)
var num10 = math.Floor(1.9)
var num11 = math.Floor(2.0)
fmt.Println("The floor value of 1.0 is", num1);
fmt.Println("The floor value of 1.1 is", num2);
fmt.Println("The floor value of 1.2 is", num3);
fmt.Println("The floor value of 1.3 is", num4);
fmt.Println("The floor value of 1.4 is", num5);
fmt.Println("The floor value of 1.5 is", num6);
fmt.Println("The floor value of 1.6 is", num7);
fmt.Println("The floor value of 1.7 is", num8);
fmt.Println("The floor value of 1.8 is", num9);
fmt.Println("The floor value of 1.9 is", num10);
fmt.Println("The floor value of 2.0 is", num11);
}
The floor value of 1.0 is 1 The floor value of 1.1 is 1 The floor value of 1.2 is 1 The floor value of 1.3 is 1 The floor value of 1.4 is 1 The floor value of 1.5 is 1 The floor value of 1.6 is 1 The floor value of 1.7 is 1 The floor value of 1.8 is 1 The floor value of 1.9 is 1 The floor value of 2.0 is 2
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.
package main
import (
"fmt"
"math"
)
func main() {
var num1 = math.Floor(1.5698)
var num2 = math.Floor(1.3093)
var num3 = math.Floor(1.0087)
var num4 = math.Floor(1.1038)
var num5 = math.Floor(1.9573)
var num6 = math.Floor(1.7462)
var num7 = math.Floor(1.6427)
var num8 = math.Floor(1.4128)
var num9 = math.Floor(1.8099)
var num10 = math.Floor(1.2743)
var num11 = math.Floor(2.0001)
fmt.Println("The floor value of 1.5698 is", num1);
fmt.Println("The floor value of 1.3093 is", num2);
fmt.Println("The floor value of 1.0087 is", num3);
fmt.Println("The floor value of 1.1038 is", num4);
fmt.Println("The floor value of 1.9573 is", num5);
fmt.Println("The floor value of 1.7462 is", num6);
fmt.Println("The floor value of 1.6427 is", num7);
fmt.Println("The floor value of 1.4128 is", num8);
fmt.Println("The floor value of 1.8099 is", num9);
fmt.Println("The floor value of 1.2743 is", num10);
fmt.Println("The floor value of 2.0001 is", num11);
}
The floor value of 1.5698 is 1 The floor value of 1.3093 is 1 The floor value of 1.0087 is 1 The floor value of 1.1038 is 1 The floor value of 1.9573 is 1 The floor value of 1.7462 is 1 The floor value of 1.6427 is 1 The floor value of 1.4128 is 1 The floor value of 1.8099 is 1 The floor value of 1.2743 is 1 The floor value of 2.0001 is 2
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.
package main
import (
"fmt"
"math"
)
func main() {
var num1 = math.Floor(-1.5698)
var num2 = math.Floor(-1.3093)
var num3 = math.Floor(-1.0087)
var num4 = math.Floor(-1.1038)
var num5 = math.Floor(-1.9573)
var num6 = math.Floor(-1.7462)
var num7 = math.Floor(-1.6427)
var num8 = math.Floor(-1.4128)
var num9 = math.Floor(-1.8099)
var num10 = math.Floor(-1.2743)
var num11 = math.Floor(-2.0001)
fmt.Println("The floor value of -1.5698 is", num1);
fmt.Println("The floor value of -1.3093 is", num2);
fmt.Println("The floor value of -1.0087 is", num3);
fmt.Println("The floor value of -1.1038 is", num4);
fmt.Println("The floor value of -1.9573 is", num5);
fmt.Println("The floor value of -1.7462 is", num6);
fmt.Println("The floor value of -1.6427 is", num7);
fmt.Println("The floor value of -1.4128 is", num8);
fmt.Println("The floor value of -1.8099 is", num9);
fmt.Println("The floor value of -1.2743 is", num10);
fmt.Println("The floor value of -2.0001 is", num11);
}
The floor value of -1.5698 is -2 The floor value of -1.3093 is -2 The floor value of -1.0087 is -2 The floor value of -1.1038 is -2 The floor value of -1.9573 is -2 The floor value of -1.7462 is -2 The floor value of -1.6427 is -2 The floor value of -1.4128 is -2 The floor value of -1.8099 is -2 The floor value of -1.2743 is -2 The floor value of -2.0001 is -3
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.
package main
import (
"fmt"
"math"
)
func main() {
// Positive ∞ infinite value
n := math.Inf(+1)
fmt.Println("The floor value of +Inf is", math.Floor(n));
// Negative ∞ infinite value
m := math.Inf(-1)
fmt.Println("The floor value of -Inf is", math.Floor(m));
}
The floor value of +Inf is +Inf The floor value of -Inf is -Inf
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.
package main
import (
"fmt"
"math"
)
func main() {
metres, perStrap := 8.5, 1.2
fmt.Printf("%.0f straps", math.Floor(metres / perStrap))
}
7 straps
Seven straps use 8.4 metres. The last tenth of a metre cannot make another strap.
Reference: Go numeric operations