2013-10-07 23 views
64

整数値をfloat64型に変換するにはどうすればよいですか?Golangの整数を浮動小数点数に変換する

私は

float(integer_value) 

を試してみました。しかし、これは動作しません。 Golang.orgでこれを行うパッケージを見つけることはできません。

整数値からfloat64値を取得するにはどうすればよいですか?

答えて

94

floatタイプはありません。あなたが望むように見えるfloat64。単精度浮動小数点値だけが必要な場合は、float32を使用することもできます。

package main 

import "fmt" 

func main() { 
    i := 5 
    f := float64(i) 
    fmt.Printf("f is %f\n", f) 
} 
20

これは、a link to the golang documentation which describes all typesです。あなたの場合、数値型です:

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 

これは、float64(integer_value)を使用する必要があることを意味します。

関連する問題