Devdit
 

not enough arguments in call to strconv.ParseFloat have (string) want (string, int) Go คือ วิธีแก้ไข

1.2K

รันโค้ด Go (Golang) แปลงข้อความ (String) เป็นเลขทศนิยม (Float) แต่รันไม่สำเร็จขึ้นข้อความว่า not enough arguments in call to strconv.ParseFloat have (string) want (string, int) แบบนี้ต้องแก้ไขอย่างไร

package main
import (
    "fmt"
	"strconv"
)

func main() {
	f := "123.36"
	s, _ := strconv.ParseFloat( f )
	fmt.Println( s )
}

 

วิธีแก้ไข

คำสั่ง strconv.ParseFloat รับค่าพารามิเตอร์ 2 ค่าคือ ตัวแปรชนิดข้อความ (String) และขนาดของตัวเลขทศนิยม ซึ่งแนะนำให้กำหนดเป็น 64 คือ float64 แก้ไขโค้ดได้ดังนี้

package main
import (
    "fmt"
	"strconv"
)

func main() {
	f := "123.36"
	s, _ := strconv.ParseFloat( f, 64 )
	fmt.Println( s )
}
แก้ไข 2 ปีที่แล้ว
ชอบ
ลิ้งก์
แชร์
Devdit มีช่อง YouTube แล้ว
เราสร้างวิดีโอเกี่ยวกับเทคโนโลยี ทำตามง่ายๆ