【发布时间】:2016-02-24 10:38:42
【问题描述】:
我在 go 中有 4 个浮点值(startLat、startLon、endLat、endLon)。我想将这些值附加(替换)到以下字符串:
var etaString = []byte(`{"start_latitude":"` + startLat + `","start_longitude":"` + startLon + `","end_latitude":"` + endLat + `","end_longitude":"` + endLon }`)
在这样做之前,我必须将它们类型转换为字符串。
startLat := strconv.FormatFloat(o.Coordinate.Longitude, 'g', 1, 64)
但是,当我这样做时,我将这些参数的值作为"4e+01 -1e+02 4e+01 -1e+02"
但我只想要这样的东西:“64.2345”。
我怎样才能做到这一点? TIA :)
【问题讨论】:
-
fmt.Sprintf("%.4f", f)根据所需格式将 float64 设置为字符串。见stackoverflow.com/a/62753031/12817546。strconv.ParseFloat(s, 64)根据所需格式将字符串设置为 float64。见stackoverflow.com/a/62740786/12817546。
标签: string go floating-point typecast-operator