golang的string、int、int64、float64互相转换

golang的string、int、int64、float64互相转换

编码文章call10242025-03-09 15:59:2838A+A-
 a1 := 15
// int 转 string
s1 := strconv.Itoa(a1)
// int 转 string
s2 := fmt.Sprintf("%d", a1)

var a2 int64 = 10
// int64 转 string
s3 := strconv.FormatInt(a2, 10)

// string 转 int
a3, _ := strconv.Atoi(s1)
// string 转 int64
a4, _ := strconv.ParseInt(s2, 10, 64)

// float64 转 int64
var a5 float64 = 1.5
a6 := int64(a5)
// float64 转 int
a7 := int(a5)
// float64 转 string,如果是float32,则后面就改成32
s5 := strconv.FormatFloat(a5, 'E', -1, 64)
// string 转 float64 32位同样更改其中的参数即可
a8, _ := strconv.ParseFloat(s5, 64)

// 有小数点的字符串转int64 如 '123.000' 转为123
//  先字符串转float64。再 float64 转int64
var aStr = "123.000"
 aFloat64 := Fatof64(aStr)
 aInt64 := Int64(aFloat64)


//! 字符串转float64
func Fatof64(s string) float64 {
	num, _ := strconv.ParseFloat(s, 64)
	return num
}

点击这里复制本文地址 以上内容由文彬编程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

文彬编程网 © All Rights Reserved.  蜀ICP备2024111239号-4