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:
// Golang program to illustrate how to // get the integer value of the given numbers package main import ( "fmt" "math" ) // Main function func main() { // Finding the integer // value of the given numbers // Using Trunc() function 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) // Displaying the result 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) } |
chevron_right
filter_none
Output:
Result 1: 45.0 Result 2: -119.0 Result 3: -Inf Result 4: NaN Result 5: 3.0
Example 2:
// Golang program to illustrate how to get // the integer value of the given numbers package main import ( "fmt" "math" ) // Main function func main() { // Finding the integer value // of the given numbers // Using Trunc() function nvalue_1 := math.Trunc(59.89) nvalue_2 := math.Trunc(-7.567) // Sum of the given numbers res := nvalue_1 + nvalue_2 fmt.Printf( "%.2f + %.2f = %.2f" , nvalue_1, nvalue_2, res) } |
chevron_right
filter_none
Output:
59.00 + -7.00 = 52.00