Devdit
 

cannot use i (type int) as type string in argument to strconv.ParseFloat Go คือ วิธีแก้ไข

756

รันโค้ด Go (Golang) เพื่อแปลงตัวเลข เป็นตัวเลขแบบทศนิยมแต่รันไม่สำเร็จขึ้นข้อความว่า cannot use i (type int) as type string in argument to strconv.ParseFloat แบบนี้ต้องแก้ไขอย่างไร

func main() {
	i := 90
	f, _ := strconv.ParseFloat( i, 64 )
	fmt.Print( "f = ", f )
}

 

วิธีแก้ไข

กรณีต้องการแปลงตัวเลข (int) เป็นตัวเลขทศนิยม (float) แนะนำให้ใช้คำสั่ง float64 สามารถเขียนโปรแกรมได้ดังนี้

func main() {
	i := 90
	f := float64( i )
	fmt.Print( "f = ", f )
}
เขียน 2 ปีที่แล้ว
ชอบ
ลิ้งก์
แชร์
Devdit มีช่อง YouTube แล้ว
เราสร้างวิดีโอเกี่ยวกับเทคโนโลยี ทำตามง่ายๆ