将一个整数转换为浮点数。

huangapple go评论141阅读模式
英文:

Convert an integer to a float number

问题

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

我尝试了

float(integer_value)

但是这个方法不起作用。我在Golang.org上找不到任何可以实现这个功能的包。

如何从整数值获取float64值?

英文:

How do I convert an integer value to float64 type?

I tried

float(integer_value)

But this does not work. And can't find any package that does this on Golang.org

How do I get float64 values from integer values?

答案1

得分: 221

没有float类型。看起来你想要使用float64。如果你只需要单精度浮点数,也可以使用float32

package main

import "fmt"

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

There is no float type. Looks like you want float64. You could also use float32 if you only need a single-precision floating point value.

package main

import "fmt"

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

答案2

得分: 38

为了完整起见,这里是描述所有类型的golang文档链接。在你的情况下,它是数字类型:

uint8       所有无符号8位整数的集合(0到255)
uint16      所有无符号16位整数的集合(0到65535)
uint32      所有无符号32位整数的集合(0到4294967295)
uint64      所有无符号64位整数的集合(0到18446744073709551615)

int8        所有有符号8位整数的集合(-128到127)
int16       所有有符号16位整数的集合(-32768到32767)
int32       所有有符号32位整数的集合(-2147483648到2147483647)
int64       所有有符号64位整数的集合(-9223372036854775808到9223372036854775807)

float32     所有IEEE-754 32位浮点数的集合
float64     所有IEEE-754 64位浮点数的集合

complex64   所有具有float32实部和虚部的复数的集合
complex128  所有具有float64实部和虚部的复数的集合

byte        uint8的别名
rune        int32的别名

这意味着你需要使用float64(integer_value)

英文:

Just for the sake of completeness, here is a link to the golang documentation which describes all types. In your case it is numeric 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

Which means that you need to use float64(integer_value).

答案3

得分: 6

package main

func main() {
    a := 70
    afloat := float64(a)
    fmt.Printf("a的类型是 %T\n", a) // 将会输出 int
    fmt.Printf("afloat的类型是 %T\n", afloat) // 将会输出 float64
}

请注意,上述代码是Go语言的代码,用于将整数类型转换为浮点数类型,并打印出变量的类型。

英文:

just do these

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
}

答案4

得分: 2

intutils.ToFloat32

// ToFloat32将一个整数转换为float32类型的数值
func ToFloat32(in int) float32 {
return float32(in)
}

// ToFloat64将一个整数转换为float64类型的数值
func ToFloat64(in int) float64 {
return float64(in)
}

英文:

intutils.ToFloat32

// 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)
}

答案5

得分: 2

正确的括号放置是关键:

package main

import (
	"fmt"
)

func main() {

	var payload uint32
	var fpayload float32
	payload = 1320
	
	// 正确的写法
	fpayload = float32(payload) / 100.0
	fmt.Printf("%T = %d, %T = %f\n", payload, payload, fpayload, fpayload)
	
	// 错误的写法
	fpayload = float32(payload / 100.0)
	fmt.Printf("%T = %d, %T = %f\n", payload, payload, fpayload, fpayload)
}

结果:

uint32 = 1320, float32 = 13.200000

uint32 = 1320, float32 = 13.000000

Go Playground

英文:

Proper parentheses placement is key:

package main

import (
	"fmt"
)

func main() {

	var payload uint32
	var fpayload float32
	payload = 1320
	
	// works
	fpayload = float32(payload) / 100.0
	fmt.Printf("%T = %d, %T = %f\n", payload, payload, fpayload, fpayload)
	
	// doesn't work
	fpayload = float32(payload / 100.0)
	fmt.Printf("%T = %d, %T = %f\n", payload, payload, fpayload, fpayload)
}

results:

uint32 = 1320, float32 = 13.200000

uint32 = 1320, float32 = 13.000000

The Go Playground

答案6

得分: 1

类型转换T(),其中T是所需结果的数据类型,在Go语言中非常简单。

在我的程序中,我从用户输入中扫描一个整数i,对其进行类型转换,并将其存储在变量f中。输出打印了int输入的float64等效值。Go语言还提供了float32数据类型。

代码:

package main
import "fmt"
func main() {
    var i int
    fmt.Println("输入一个整数:")
    fmt.Scanf("%d", &i)
    f := float64(i)
    fmt.Printf("整数 %d 的 float64 表示为 %f\n", i, f)
}

解决方案:

>>> 输入一个整数:
>>> 232332
>>> 整数 232332 的 float64 表示为 232332.000000
英文:

Type Conversions T() where T is the desired datatype of the result are quite simple in GoLang.

In my program, I scan an integer i from the user input, perform a type conversion on it and store it in the variable f. The output prints the float64 equivalent of the int input. float32 datatype is also available in GoLang

Code:

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)
}

Solution:

>>> Enter an Integer input:
>>> 232332
>>> The float64 representation of 232332 is 232332.000000

huangapple
  • 本文由 发表于 2013年10月8日 00:41:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/19230191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定