将整数转换为浮点数

如何将整数值转换为float64类型?

我试过了

float(integer_value)

但这不起作用。并且在Golang.org上找不到任何执行此操作的软件包

如何float64从整数值中获取值?


鸿蒙传说
浏览 451回答 3
3回答

慕娘9325324

没有float类型。看起来你想要float64。float32如果您只需要一个单精度浮点值,您也可以使用。package mainimport "fmt"func main() {    i := 5    f := float64(i)    fmt.Printf("f is %f\n", f)}

墨色风雨

为了完整起见,这里是描述所有类型的 golang 文档的链接。在您的情况下,它是数字类型:uint8       the set of all unsigned  8-bit integers (0 to 255)uint16      the set of all unsigned 16-bit integers (0 to 65535)uint32      the set of all unsigned 32-bit integers (0 to 4294967295)uint64      the set of all unsigned 64-bit integers (0 to 18446744073709551615)int8        the set of all signed  8-bit integers (-128 to 127)int16       the set of all signed 16-bit integers (-32768 to 32767)int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)int64       the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)float32     the set of all IEEE-754 32-bit floating-point numbersfloat64     the set of all IEEE-754 64-bit floating-point numberscomplex64   the set of all complex numbers with float32 real and imaginary partscomplex128  the set of all complex numbers with float64 real and imaginary partsbyte        alias for uint8rune        alias for int32这意味着您需要使用float64(integer_value).

ABOUTYOU

intutils.ToFloat32// ToFloat32 converts a int num to a float32 numfunc ToFloat32(in int) float32 {    return float32(in)}// ToFloat64 converts a int num to a float64 numfunc ToFloat64(in int) float64 {    return float64(in)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go