在 Golang 中,可以使用 math 包中的 Abs 函数来取绝对值。具体示例代码如下:

package main

import (
    "fmt"
    "math"
)

func main() {
    x := -10
    absX := math.Abs(float64(x))
    fmt.Printf("The absolute value of %d is %.0f\n", x, absX)
}

输出结果为:

The absolute value of -10 is 10
Golang 获取绝对值:使用 math.Abs 函数

原文地址: https://www.cveoy.top/t/topic/qqRT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录