在Go中将float64转换为int

Chr*_*nch 101 go

如何在Go中将float64转换为int?我知道该strconv包可用于将任何内容转换为字符串或从字符串转换,但不能在数据类型之间转换,其中一个不是字符串.我知道我可以使用fmt.Sprintf将任何东西转换为字符串,然后将strconv其转换为我需要的数据类型,但这种额外的转换似乎有点笨拙 - 有更好的方法吗?

Dav*_*son 169

package main
import "fmt"
func main() {
  var x float64 = 5.7
  var y int = int(x)
  fmt.Println(y)  // outputs "5"
}
Run Code Online (Sandbox Code Playgroud)

  • @DavidLarsen来自Go规范:“将浮点数转换为整数时,将舍弃小数(截断为零)”。([符合规范](http://golang.org/ref/spec#Conversions)) (2认同)
  • 这样的强制转换在Go中有一个可能出乎意料的问题(至少如果你来自Java):"在涉及浮点或复数值的所有非常量转换中,如果结果类型不能表示转换成功的值,但结果价值取决于实施." (https://golang.org/ref/spec#Conversions).因此,如果有效地使用非常大的值作为无穷大,并将其转换为int,则结果是未定义的(与Java不同,它是整数类型的最大值). (2认同)

Dav*_*ing 7

简单地将其强制转换为整数会截断浮点数,如果系统内部将2.0表示为1.9999999999,您将无法获得预期的结果。各种printf转换处理此问题,并在转换时正确舍入数字。因此,要获得更准确的值,转换会比您最初预期的要复杂得多:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    floats := []float64{1.9999, 2.0001, 2.0}
    for _, f := range floats {
        t := int(f)
        s := fmt.Sprintf("%.0f", f)
        if i, err := strconv.Atoi(s); err == nil {
            fmt.Println(f, t, i)
        } else {
            fmt.Println(f, t, err)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

围棋操场上的代码

  • 这是现在最好的方法,但是 [`math.Round`](https://pkg.go.dev/math#Round) 是在 go 1.10 中引入的,该版本是在上述答案发布两年后发布的。 (2认同)

sca*_*zoo 7

可能需要正确的舍入。

因此 math.Round() 是您的快速(!)朋友。根据我的测试,使用 fmt.Sprintf 和 strconv.Atois() 的方法要慢 2 个数量级,其中 float64 值矩阵旨在成为正确舍入的 int 值。

package main
import (
    "fmt"
    "math"
)
func main() {
    var x float64 = 5.51
    var y float64 = 5.50
    var z float64 = 5.49
    fmt.Println(int(math.Round(x)))  // outputs "6"
    fmt.Println(int(math.Round(y)))  // outputs "6"
    fmt.Println(int(math.Round(z)))  // outputs "5"
}
Run Code Online (Sandbox Code Playgroud)

math.Round() 确实返回了一个 float64 值,但是在之后应用了 int() 之后,到目前为止我找不到任何不匹配的地方。


Pea*_*uts 6

如果它只是从float64到int,这应该可以工作

package main

import (
    "fmt"
)

func main() {
    nf := []float64{-1.9999, -2.0001, -2.0, 0, 1.9999, 2.0001, 2.0}

    //round
    fmt.Printf("Round : ")
    for _, f := range nf {
        fmt.Printf("%d ", round(f))
    }
    fmt.Printf("\n")

    //rounddown ie. math.floor
    fmt.Printf("RoundD: ")
    for _, f := range nf {
        fmt.Printf("%d ", roundD(f))
    }
    fmt.Printf("\n")

    //roundup ie. math.ceil
    fmt.Printf("RoundU: ")
    for _, f := range nf {
        fmt.Printf("%d ", roundU(f)) 
    }
    fmt.Printf("\n")

}

func roundU(val float64) int {
    if val > 0 { return int(val+1.0) }
    return int(val)
}

func roundD(val float64) int {
    if val < 0 { return int(val-1.0) }
    return int(val)
}

func round(val float64) int {
    if val < 0 { return int(val-0.5) }
    return int(val+0.5)
}
Run Code Online (Sandbox Code Playgroud)

输出:

Round : -2 -2 -2 0 2 2 2 
RoundD: -2 -3 -3 0 1 2 2 
RoundU: -1 -2 -2 0 2 3 3 
Run Code Online (Sandbox Code Playgroud)

这是操场上的代码-https: //play.golang.org/p/HmFfM6Grqh

  • Go 有一个 Round 函数,你可以使用它四舍五入到最接近的整数:math.Round(-64.99) 将输出 -65。 (2认同)