如何使用Go将浮点数格式化为字符串

如何使用Go将浮点数格式化为字符串

问题描述:

Using Go I'm trying to find the "best" way to format a floating point number into a string. I've looked for examples however I cannot find anything that specifically answers the questions I have. All I want to do is use the "best" method to format a floating point number into a string. The number of decimal places may vary but will be known (eg. 2 or 4 or zero). An example of what I want to achieve is below. Based on the example below should I use fmt.Sprintf() or strconv.FormatFloat() or something else? and, what is the normal usage of each and differences between each?

I also don't understand the significance of using either 32 or 64 in the following which currently has 32 :

strconv.FormatFloat(float64(fResult), 'f', 2, 32)

Example:

package main

import (
    "fmt"
    "strconv"
)

func main() {

    var (
        fAmt1 float32 = 999.99
        fAmt2 float32 = 222.22
    )

    var fResult float32 = float32(int32(fAmt1*100) +int32(fAmt2*100)) / 100

    var sResult1 string = fmt.Sprintf("%.2f", fResult)

    println("Sprintf value = " + sResult1)

    var sResult2 string = strconv.FormatFloat(float64(fResult), 'f', 2, 32)

    println("FormatFloat value = " + sResult2)

}

使用Go我正在尝试找到将浮点数格式化为字符串的“最佳”方法。 我一直在寻找示例,但是找不到任何可以具体回答我所遇到问题的东西。 我要做的就是使用“最佳”方法将浮点数格式化为字符串。 小数位数可能有所不同,但会知道(例如2或4或零)。 下面是我想要实现的示例。 根据以下示例,我应该使用 fmt.Sprintf()\ strconv.FormatFloat()\也没有\没有其他内容吗? ,它们的正常用法是什么?它们之间的区别是什么? p>

我也不了解使用的重要性 以下是32或64,目前具有32: p>

  strconv.FormatFloat(float64(fResult),'f',2,32)
  code>  
 
 

示例: p>

 包main 
 
import(
“ fmt” 
“ strconv” 
)
 
func  main(){
 
 var(
 fAmt1 float32 = 999.99 
 fAmt2 float32 = 222.22 
)
 
 var fResult float32 = float32(int32(fAmt1 * 100)+ int32(fAmt2 * 100))/  100 
 
 var sResult1字符串= fmt.Sprintf(“%。2f”,fResult)
 
 println(“ Sprintf value =” + sResult1)
 
 var sResult2字符串= strconv.FormatFloat(float64(fResult  ),'f',2,32)
 
 println(“ FormatFloat value =” + s  Result2)
 
} 
  code>  pre> 
  div>

Both fmt.Sprintf and strconv.FormatFloat use the same string formatting routine under the covers, so should give the same results.

If the precision that the number should be formatted to is variable, then it is probably easier to use FormatFloat, since it avoids the need to construct a format string as you would with Sprintf. If it never changes, then you could use either.

The last argument to FormatFloat controls how values are rounded. From the documentation:

It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64)

So if you are working with float32 values as in your sample code, then passing 32 is correct.

You will have with Go 1.12 (February 2019) and the project cespare/ryu a faster alternative to strconv:

Ryu is a Go implementation of Ryu, a fast algorithm for converting floating-point numbers to strings.
It is a fairly direct Go translation of Ulf Adams's C library.

The strconv.FormatFloat latency is bimodal because of an infrequently-taken slow path that is orders of magnitude more expensive (issue 15672).

The Ryu algorithm requires several lookup tables.
Ulf Adams's C library implements a size optimization (RYU_OPTIMIZE_SIZE) which greatly reduces the size of the float64 tables in exchange for a little more CPU cost.

For a small fraction of inputs, Ryu gives a different value than strconv does for the last digit.
This is due to a bug in strconv: issue 29491.

Go 1.12 might or might not include that new implementation directly in strconv, but if it does not, you can use this project for faster conversion.