Welcome To Golang By Example

Remove decimal points from a float in Go (Golang)

Table of Contents

Overview

math package of GO provides a Trunc method that can be used to remove decimal points of a float and convert it to an integer

Below is the signature of the function. It takes input a float and also returns a float.

func Trunc(x float64) float64\

Code:

package main

import (
    "fmt"
    "math"
)

func main() {
    res := math.Trunc(1.6)
    fmt.Println(res)

    res = math.Trunc(-1.6)
    fmt.Println(res)

    res = math.Trunc(1)
    fmt.Println(res)
}

Output:

1
-1
1