Abs()
Published Jun 9, 2023
Contribute to Docs
The Abs() function returns the absolute value of a given number.
Syntax
import "math"
result := math.Abs(number)
Where result
is the absolute value of number
.
- The result of
Abs(-Inf)
is+Inf
- The result of
Abs(+Inf)
is+Inf
- The result of
Abs(NaN)
isNaN
Example
The following calculates the absolute value of -255
and prints out the result:
package mainimport ("fmt""math")func main() {number := math.Abs(-255)fmt.Printf("%.1f\n", number)}
The output will be:
255.0
Codebyte Example
The following example is runnable and shows the Abs()
function handling an infinite value.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.