Menu Close

Go – How to get the integer value of number in go

Here, We will learn how to get the integer value of number in go. We can get it by using Trunc() function in math package in go golang.

Function prototype:

func Trunc(x float64) float64

Return value:

Trunc() function in math package returns 
the integer value of x.

Example with code:

package main

import (
  "fmt"
  "math"
)

func main() {

  no := math.Trunc(1.2)
  fmt.Println(no)

  no = math.Trunc(-2.7)
  fmt.Println(no)

  no = math.Trunc(0.0)
  fmt.Println(no)

  no = math.Trunc(-2.9)
  fmt.Println(no)

  no = math.Trunc(2)
  fmt.Println(no)
}

Output:

1
-2
0
-2
2

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, math

Leave a Reply

Your email address will not be published. Required fields are marked *