Golang:是否有任何标准的库将float64转换为固定宽度的字符串,最大有效位数? [英] Golang: is there any standard library to convert float64 to string with fix width with maximum number of significant digits?

查看:258
本文介绍了Golang:是否有任何标准的库将float64转换为固定宽度的字符串,最大有效位数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们需要打印12个固定宽度的表格,我们需要打印 float64 个数字:

  fmt.Printf(%12.6g\\\
,9.405090880450127e + 119)//9.40509e +119
fmt.Printf(%12.6g\\\
,0.1234567890123) //0.123457
fmt.Printf(%12.6g\\\
,123456789012.0)//1.23457e + 11

我们更喜欢0.1234567890来0.123457我们输了6位有效数字。

我们更喜欢123456789012来1.23457e + 11我们输了6位有效数字。 >

是否有任何标准库将 float64 转换为 string 宽度与有效数字的最大数量?
在此先感谢。


基本上你有两种输出格式:科学记数法或常规格式。这两种格式之间的转折点是 1e12

所以,如果 x > = 1e12 。在这两个分支你可以做一个0分数的格式,看看这个数字是多少,所以你可以计算有多少分数的数字将适合12的宽度,所以你可以构造最终的格式字符串,使用计算的精度。

科学记数法也需要进行预检(%g ),因为指数的宽度(例如 e + 1 e + 10 e + 100 )。

下面是一个示例实现。这是为了让你开始,但它并不意味着处理所有情况下,它不是最有效的解决方案(但相对简单,做这项工作):

  // format12格式x为12个字符长。 
func format12(x float64)string {
if x> = 1e12 {
//检查有多少小数位适合:
s:= fmt.Sprintf( %.g,x)
format:= fmt.Sprintf(%% 12.%dg,12-len(s))
return fmt.Sprintf(format,x)


//检查看有多少小数位适合:
s:= fmt.Sprintf(%。0f,x)
len(s)= = 12 {
return s
}
format:= fmt.Sprintf(%%% d。%df,len(s),12-len(s)-1)
return fmt.Sprintf(format,x)
}

测试:

  fs:= [] float64 {0,1234.567890123,0.1234567890123,123456789012.0,1234567890123.0,
9.405090880450127e + 9,9.405090880450127 (格式12(f))
}

输出(在去操场): p>

  0.0000000000 
0.1234567890
1234.5678901
123456789012
1.234568e + 12
9405090880.5
9.405091e + 19
9.40509e + 119


Imagine for printing in a 12 fixed width table we need printing float64 numbers:

fmt.Printf("%12.6g\n", 9.405090880450127e+119) //"9.40509e+119"
fmt.Printf("%12.6g\n", 0.1234567890123)        //"    0.123457"
fmt.Printf("%12.6g\n", 123456789012.0)         //" 1.23457e+11"

We prefer 0.1234567890 to " 0.123457" we lose 6 significant digits.
We prefer 123456789012 to " 1.23457e+11" we lose 6 significant digits.

Is there any standard library to convert float64 to string with fix width with maximum number of significant digits? Thanks in Advance.

解决方案

Basically you have 2 output formats: either a scientific notation or a regular form. The turning point between those 2 formats is 1e12.

So you can branch if x >= 1e12. In both branches you may do a formatting with 0 fraction digits to see how long the number will be, so you can calculate how many fraction digits will fit in for 12 width, and so you can construct the final format string, using the calculated precision.

The pre-check is required in the scientific notation too (%g), because the width of exponent may vary (e.g. e+1, e+10, e+100).

Here is an example implementation. This is to get you started, but it does not mean to handle all cases, and it is not the most efficient solution (but relatively simple and does the job):

// format12 formats x to be 12 chars long.
func format12(x float64) string {
    if x >= 1e12 {
        // Check to see how many fraction digits fit in:
        s := fmt.Sprintf("%.g", x)
        format := fmt.Sprintf("%%12.%dg", 12-len(s))
        return fmt.Sprintf(format, x)
    }

    // Check to see how many fraction digits fit in:
    s := fmt.Sprintf("%.0f", x)
    if len(s) == 12 {
        return s
    }
    format := fmt.Sprintf("%%%d.%df", len(s), 12-len(s)-1)
    return fmt.Sprintf(format, x)
}

Testing it:

fs := []float64{0, 1234.567890123, 0.1234567890123, 123456789012.0, 1234567890123.0,
    9.405090880450127e+9, 9.405090880450127e+19, 9.405090880450127e+119}

for _, f := range fs {
    fmt.Println(format12(f))
}

Output (try it on the Go Playground):

0.0000000000
0.1234567890
1234.5678901
123456789012
1.234568e+12
9405090880.5
9.405091e+19
9.40509e+119

这篇关于Golang:是否有任何标准的库将float64转换为固定宽度的字符串,最大有效位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆