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 numbers
float64 the set of all IEEE-754 64-bit floating-point numbers
complex64 the set of all complex numbers with float32 real and imaginary parts
complex128 the set of all complex numbers with float64 real and imaginary parts
byte alias for uint8
rune alias for int32
package main
func main(){
a:= 70
afloat := float64(a)
fmt.Printf("type of a is %T\n", a) // will int
fmt.Printf("type of a is %T\n", afloat) //will float64
}
// ToFloat32 converts a int num to a float32 num
func ToFloat32(in int) float32 {
return float32(in)
}
// ToFloat64 converts a int num to a float64 num
func ToFloat64(in int) float64 {
return float64(in)
}
package main
import "fmt"
func main() {
var i int
fmt.Println("Enter an Integer input: ")
fmt.Scanf("%d", &i)
f := float64(i)
fmt.Printf("The float64 representation of %d is %f\n", i, f)
}
解决方案:
>>> Enter an Integer input:
>>> 232332
>>> The float64 representation of 232332 is 232332.000000
6条答案
按热度按时间6rvt4ljy1#
没有
float
类型。看起来您需要float64
。如果您只需要单精度浮点值,也可以使用float32
。20jt8wwn2#
为了完整起见,这里有一个golang文档的链接,它描述了所有类型。在您的例子中,它是数字类型:
这意味着您需要使用
float64(integer_value)
。quhf5bfb3#
就做这些
h6my8fg24#
intutils.ToFloat32
mefy6pfw5#
正确放置括号是关键:
结果:
单位32 = 1320,浮点数32 = 13.200000
单位32 = 1320,浮点数32 = 13.000000
The Go Playground
olhwl3o26#
类型转换T(),其中T是结果所需的数据类型,在GoLang中非常简单。
在我的程序中,我从用户输入中扫描一个整数i,对其执行类型转换并将其存储在变量f中。
代码:
解决方案: