Menu Close

Go – How to return the absolute value of a number in go

Here, We will learn how to get the absolute value of a number in go. We can use Abs() function of math package to get absolute value for the number.

Function proto type:

func Abs(no float64) float64

Return value:

Abs() function of math package returns
the absolute value of given number.

Example with code:

package main

import (
  "fmt"
  "math"
)

func main() {
    
  no := math.Abs(-10)
  fmt.Printf("%.2f\n", no)

  no = math.Abs(10)
  fmt.Printf("%.2f\n", no)
	
  no = math.Abs(-10.0)
  fmt.Printf("%.2f\n", no)
  
}

Output:

10.00
10.00
10.00

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 *