Convert float to string in go lang as per the required format

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

Convert float to string in go lang as per the required format

问题

我有4个浮点数值(startLat,startLon,endLat,endLon)在Go语言中。我想将这些值附加(替换)到下面的字符串中:

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"

我该如何实现这个目标?

英文:

I have 4 float values(startLat,startLon,endLat,endLon)in Go. I want to append(replace) these values to the below string:

var etaString = []byte(`{"start_latitude":"` + startLat + `","start_longitude":"` + startLon + `","end_latitude":"` + endLat + `","end_longitude":"` + endLon }`)

I have to typecast them to string before doing so.

startLat := strconv.FormatFloat(o.Coordinate.Longitude, 'g', 1, 64)

However, when I do so, I get the values of these parameters as "4e+01 -1e+02 4e+01 -1e+02"
But I just want something like this: "64.2345".

How can I achieve this?

答案1

得分: 11

> strconv 包
>
> import "strconv" > func FormatFloat
>
> func FormatFloat(f float64, fmt byte, prec, bitSize int) string
>
> FormatFloat 函数将浮点数 f 转换为字符串,根据格式 fmt 和精度 prec 进行转换。它假设原始值是从 bitSize 位的浮点数(32 位为 float32,64 位为 float64)获得的,并对结果进行四舍五入。
>
> 格式 fmt 可以是以下之一:'b'(-ddddp±ddd,二进制指数)、'e'(-d.dddde±dd,十进制指数)、'E'(-d.ddddE±dd,十进制指数)、'f'(-ddd.dddd,无指数)、'g'(对于大指数为'e',否则为'f')、'G'(对于大指数为'E',否则为'f')。
>
> 精度 prec 控制由 'e'、'E'、'f'、'g' 和 'G' 格式打印的数字(指数除外)的位数。对于 'e'、'E' 和 'f',它是小数点后的位数。对于 'g' 和 'G',它是总位数。特殊的精度 -1 使用最少的位数,以便 ParseFloat 函数能够精确返回 f。

使用精度 -1,而不是 1。使用格式 f,而不是 g,以避免对大指数使用指数形式(参见HectorJ的评论)。

startLat := strconv.FormatFloat(o.Coordinate.Longitude, 'f', -1, 64)

例如:

package main

import (
	"fmt"
	"strconv"
)

func main() {
	f := 64.2345
	s := strconv.FormatFloat(f, 'g', 1, 64)
	fmt.Println(s)
	s = strconv.FormatFloat(f, 'f', -1, 64)
	fmt.Println(s)
}

输出:

6e+01
64.2345
英文:

> Package strconv
>
> import "strconv" > func FormatFloat
>
> func FormatFloat(f float64, fmt byte, prec, bitSize int) string
>
> FormatFloat converts the floating-point number f to a string,
> according to the format fmt and precision prec. It rounds the result
> assuming that the original was obtained from a floating-point value of
> bitSize bits (32 for float32, 64 for float64).
>
> The format fmt is one of 'b' (-ddddp±ddd, a binary exponent), 'e'
> (-d.dddde±dd, a decimal exponent), 'E' (-d.ddddE±dd, a decimal
> exponent), 'f' (-ddd.dddd, no exponent), 'g' ('e' for large exponents,
> 'f' otherwise), or 'G' ('E' for large exponents, 'f' otherwise).
>
> The precision prec controls the number of digits (excluding the
> exponent) printed by the 'e', 'E', 'f', 'g', and 'G' formats. For 'e',
> 'E', and 'f' it is the number of digits after the decimal point. For
> 'g' and 'G' it is the total number of digits. The special precision -1
> uses the smallest number of digits necessary such that ParseFloat will
> return f exactly.

Use a precision of -1, not 1. Use a format of f, not g to avoid exponent form for large exponents (see HectorJ's comment).

startLat := strconv.FormatFloat(o.Coordinate.Longitude, 'f', -1, 64)

For example,

package main

import (
	"fmt"
	"strconv"
)

func main() {
	f := 64.2345
	s := strconv.FormatFloat(f, 'g', 1, 64)
	fmt.Println(s)
	s = strconv.FormatFloat(f, 'f', -1, 64)
	fmt.Println(s)
}

Output:

6e+01
64.2345

答案2

得分: 0

其他选项:

package main
import "fmt"

func main() {
   n := 64.2345
   { // 示例 1
      s := fmt.Sprint(n)
      fmt.Println(s == "64.2345")
   }
   { // 示例 2
      s := fmt.Sprintf("%v", n)
      fmt.Println(s == "64.2345")
   }
   { // 示例 3
      s := fmt.Sprintf("%g", n)
      fmt.Println(s == "64.2345")
   }
}
英文:

Some other options:

package main
import "fmt"

func main() {
   n := 64.2345
   { // example 1
      s := fmt.Sprint(n)
      fmt.Println(s == "64.2345")
   }
   { // example 2
      s := fmt.Sprintf("%v", n)
      fmt.Println(s == "64.2345")
   }
   { // example 3
      s := fmt.Sprintf("%g", n)
      fmt.Println(s == "64.2345")
   }
}

huangapple
  • 本文由 发表于 2015年11月22日 14:03:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/33852140.html
匿名

发表评论

匿名网友

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

确定