Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Remove decimal points from a float in Go (Golang)

Posted on March 25, 2023March 26, 2023 by admin

Table of Contents

  • Overview
  • Code:

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
  • go
  • math
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP