integer value of float Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/integer-value-of-float/ Wed, 25 Mar 2020 17:50:55 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 integer value of float Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/integer-value-of-float/ 32 32 159787465 Get Integer value of a float in Go (Golang) https://vikasboss.github.io/integer-value-of-float/ https://vikasboss.github.io/integer-value-of-float/#respond Wed, 25 Mar 2020 17:47:11 +0000 https://vikasboss.github.io/?p=1816 Overview math package of GO provides a Trunc method that can be used to get integer value of a float Below is the signature of the function. It takes input a float...

The post Get Integer value of a float in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math package of GO provides a Trunc method that can be used to get integer value of a float

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

func Trunc(x float64) float64

Some special cases of Trunc function are

  • Trunc(±0) = ±0
  • Trunc(±Inf) = ±Inf
  • Trunc(NaN) = NaN

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

The post Get Integer value of a float in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/integer-value-of-float/feed/ 0 1816