Skip to content
Home » How to print the absolute value of float number in Golang

How to print the absolute value of float number in Golang

Learn about How to print the absolute value of float number in Golang in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

How to print the absolute value of float number in Golang

package main
import "fmt"
import "math"

func main() {
    var val float64 = -19.25
    
    fmt.Printf("Absolute value of %f is %f",val,math.Abs(val)) 
}

In the above program, we declare the package main. The main package instructs the Go language compiler that the package must be compiled and an executable file generated. We imported the fmt package, which contains the files of the fmt package, and then we can use a function related to the fmt package. Here, we also included the math package to use math-related functions. Here, we used the Abs() function of the math package to find the absolute value of the specified float variable and print the result on the console screen.

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
How to demonstrate the use of Printf() and Scanf() function in Golang
How to get the ASCII value of a character in Golang