Finding the Integer Value of Specified Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. You can find the integer value of the specified number with the help of Trunc() function provided by the math package. So, you need to add a math package in your program with the help of the import keyword to access the Trunc() function.
Syntax:
func Trunc(a float64) float64
- If you pass -Inf or +Inf in this function like Trunc(-Inf) or Trunc(+Inf), then this function will return -Inf or +Inf.
- If you pass -0 or +0 in this function like Trunc(-0) or Trunc(+0), then this function will return -0 or +0.
- If you pass NaN in this function like Trunc(NaN), then this function will return NaN.
Example 1:
package main
import (
"fmt"
"math"
)
func main() {
res_1 := math.Trunc(45.98)
res_2 := math.Trunc(-119.98)
res_3 := math.Trunc(math.Inf(-1))
res_4 := math.Trunc(math.NaN())
res_5 := math.Trunc(math.Pi)
fmt.Printf( "Result 1: %.1f" , res_1)
fmt.Printf( "\nResult 2: %.1f" , res_2)
fmt.Printf( "\nResult 3: %.1f" , res_3)
fmt.Printf( "\nResult 4: %.1f" , res_4)
fmt.Printf( "\nResult 5: %.1f" , res_5)
}
|
Output:
Result 1: 45.0
Result 2: -119.0
Result 3: -Inf
Result 4: NaN
Result 5: 3.0
Example 2:
package main
import (
"fmt"
"math"
)
func main() {
nvalue_1 := math.Trunc(59.89)
nvalue_2 := math.Trunc(-7.567)
res := nvalue_1 + nvalue_2
fmt.Printf( "%.2f + %.2f = %.2f" ,
nvalue_1, nvalue_2, res)
}
|
Output:
59.00 + -7.00 = 52.00
Last Updated :
13 Apr, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...